Converting List<string> to EntityFramework column/field list

匿名 (未验证) 提交于 2019-12-03 01:07:01

问题:

Using EntityFramework, I can get a list of entities of a certain type using the following syntax:

List customers = ((IQueryable)myEntities.Customers      .Where(c => c.Surname == strSurname)      .OrderBy(c => c.Surname)).ToList(); 

I can then do something like this to end up with only the data I'm interested in:

var customerSummaries = from s in customers      select new      {         s.Surname, s.FirstName, s.Address.Postcode      }; 

I'm given a list of strings (based on user selection) of the fields (and tables where necessary) that comprise the requested summarized data. Eg for the above 'customerSummary' the list of strings provided would be: "Surname", "FirstName", "Address.Postcode".

My question is: How do I convert that list of strings into the syntax needed to extract only the specified fields?

If that can't be done, what would be a better type (than string) for the list of columns so I can extract the right info?

I guess I need to know the type that an EF [entity|table]'s [member|column|field] is, if that makes sense.

EDIT:

I tried the suggested answer - dynamic linq - using the following syntax

string parmList = "Surname, Firstname, Address.Postcode"; var customers = myEntities.Customers.Select(parmList)   .OrderBy("Address.Postcode"); 

but this results in: EntitySqlException was unhandled. 'Surname' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly.

So, a follow-up question. Am I using Select properly? I have only seen examples using the Where and OrderBy clauses, but I think I'm doing it right based on those.

If my Select syntax is not the problem, can anyone see what is?

Edit 2: It was upside down. This works:

string parmList = "Surname, Firstname, Address.Postcode"; var customers = myEntities.Customers   .OrderBy("Address.Postcode")   .Select(parmList); 

回答1:

You can use dynamic linq, check it in here. It is also available on nuget



回答2:

I suggest dynamic linq as @Cuong already posted.

But for simple Select projections and as a hand-made exercise in expressions...

Using LinqRuntimeTypeBuilder from How to create LINQ Expression Tree to select an anonymous type
(see the comments there for 'why' is that necessary)

And add this...

public static IQueryable SelectDynamic(this IQueryable source, IEnumerable fieldNames) {     Dictionary sourceProperties = new Dictionary();     foreach (var propertyPath in fieldNames)     {         var props = propertyPath.Split('.');         var name = props.Last();         PropertyInfo[] infos;         if (sourceProperties.TryGetValue(name, out infos))             name = string.Join("", props);         sourceProperties[name] = source.ElementType.GetDeepProperty(props);     }      Type dynamicType = LinqRuntimeTypeBuilder.GetDynamicType(sourceProperties.ToDictionary(x => x.Key, x => x.Value.Last().PropertyType));      ParameterExpression sourceItem = Expression.Parameter(source.ElementType, "t");     IEnumerable bindings = dynamicType.GetFields()         .Select(p => Expression.Bind(p, sourceItem.MakePropertyExpression(sourceProperties[p.Name]))).OfType();      Expression selector = Expression.Lambda(Expression.MemberInit(         Expression.New(dynamicType.GetConstructor(Type.EmptyTypes)), bindings), sourceItem);      MethodCallExpression selectExpression = Expression.Call(typeof(Queryable), "Select", new Type[] { source.ElementType, dynamicType }, Expression.Constant(source), selector);     return Expression.Lambda(selectExpression).Compile().DynamicInvoke() as IQueryable; }  public static PropertyInfo[] GetDeepProperty(this Type type, params string[] props) {     List list = new List();     foreach (var propertyName in props)     {         var info = type.GetProperty(propertyName);         type = info.PropertyType;         list.Add(info);     }     return list.ToArray(); }  public static Expression MakePropertyExpression(this ParameterExpression sourceItem, PropertyInfo[] properties) {     Expression property = sourceItem;     foreach (var propertyInfo in properties)         property = Expression.Property(property, propertyInfo);     return property; } 

Use it like e.g.:

public static IEnumerable      SelectAsEnumerable(this IQueryable entitySet, params string[] propertyPath) {     return entitySet.SelectDynamic(propertyPath) as IEnumerable; } var list = db.YourEntity.SelectAsEnumerable("Name", "ID", "TestProperty.ID").ToList(); 

Note:
You can use similar approach to expand for OrderBy etc. - but this wasn't meant to cover all angles (or any real queries)

Deep Property Expressions from this post of mine




标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!