Building Dynamic LINQ Queries based on Combobox Value

Assuming: public class Person { public string LastName { get; set; } } IQueryable<Person> collection; your query: var query = from p in collection where p.LastName == textBox.Text select p; means the same as: var query = collection.Where(p => p.LastName == textBox.Text); which the compiler translates from an extension method to: var query = Queryable.Where(collection, … Read more

How do I do a left outer join with Dynamic Linq?

Add void DefaultIfEmpty(); to interface IEnumerableSignatures Then use public static object DefaultIfEmpty(this IQueryable source) { if (source == null) throw new ArgumentNullException(“source”); return source.Provider.Execute( Expression.Call( typeof(Queryable), “DefaultIfEmpty”, new Type[] { source.ElementType }, source.Expression)); } Then you have a call like var qry = Foo.GroupJoin(Bar, “outer.Id”, “inner.Id”, “new(outer.Id as Foo, group as Bars)”).SelectMany(“Bars.DefaultIfEmpty()”, “new(outer.Foo as Foo, … Read more

Sortable JqGrid using LINQ to MySQL (DbLinq) and Dynamic LINQ – Orderby doesn’t work

Try with the following public ActionResult All(string sidx, string sord, int page, int rows) { IQueryable<Ticket> repository = ZTRepository.GetAllTickets(); int totalRecords = repository.Count(); // first sorting the data as IQueryable<Ticket> without converting ToList() IQueryable<Ticket> orderdData = repository; System.Reflection.PropertyInfo propertyInfo = typeof(Ticket).GetProperty (sidx); if (propertyInfo != null) { orderdData = String.Compare(sord,”desc”,StringComparison.Ordinal) == 0 ? (from x … Read more

System.LINQ.Dynamic: Select(” new (…)”) into a List (or any other enumerable collection of )

First, you’ll access the current grouped value as Key in your Select clause: .Select(“new (Key as Group, Sum(Value) as TotalValue)”); That should make your query work. The harder question is how to turn the returned objects, which will have a dynamically generated type that inherits from DynamicClass, into a static type. Option 1: Use reflection … Read more

LINQ : Dynamic select

You can do this by dynamically creating the lambda you pass to Select: Func<Data,Data> CreateNewStatement( string fields ) { // input parameter “o” var xParameter = Expression.Parameter( typeof( Data ), “o” ); // new statement “new Data()” var xNew = Expression.New( typeof( Data ) ); // create initializers var bindings = fields.Split( ‘,’ ).Select( o … Read more