转换列表<串GT;到的EntityFramework列/字段列表 [英] Converting List<string> to EntityFramework column/field list

查看:145
本文介绍了转换列表<串GT;到的EntityFramework列/字段列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的EntityFramework,我可以使用下面的语法某种类型的实体列表:

 列表<客户>客户=((IQueryable的<客户>)myEntities.Customers 
。凡(C => c.Surname == strSurname)
.OrderBy(C => c.Surname))了ToList<客户与GT ;();



然后我可以做这样的事情,只有我感兴趣的数据来结束:

  VAR customerSummaries =从s在顾客
选择新的
{
s.Surname,S .FirstName,s.Address.Postcode
};



我给字符串列表秒(根据用户选择)的字段(和表如需要)组成该请求的汇总数据。例如,对于上面的'customerSummary字符串 S公司是列表:姓氏,名字,Address.Postcode



我的问题是:我如何把字符串该列表将只提取指定字段所需要的语法?



如果不能完成,这将是对列的列表更好的类型(不是字符串),所以我可以提取正确的信息?



我想我需要知道的EF [实体|表]类型的。[会员|栏|现场]是,如果是有道理的。



编辑:



我尝试了建议答案 - 动态LINQ - 使用下面的语法

 字符串parmList =姓氏,名字,Address.Postcode 
VAR的客户= myEntities.Customers.Select(parmList)
.OrderBy(Address.Postcode);



但这会导致: EntitySqlException了未处理。 姓无法在当前范围或上下文来解决。确保所有被引用的变量范围,所需的模式被加载,并且命名空间正确地引用。



所以,后续追问。我使用选择是否正确?我只看到了使用例子其中,排序依据条款,但我想我这样做是正确基于这些



如果我选择语法是没有问题的,任何人都可以看到的是什么?



编辑2:这是本末倒置。这工作:

 字符串parmList =姓氏,名字,Address.Postcode 
VAR的客户= myEntities.Customers
.OrderBy(Address.Postcode)
。选择(parmList);


解决方案

您可以使用的动态LINQ ,检查它的此处。它也可在的NuGet


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

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

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);

解决方案

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

这篇关于转换列表&LT;串GT;到的EntityFramework列/字段列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆