动态构造时如何从 IQueryable 访问列?
我正在使用此处提供的 System.Linq.Data 库 - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq- Dynamic-query-library.aspx
我有以下查询,效果很好并返回 Iqueryable
IQueryable customer =
ctx.Customers.Where(cust => true).Select("new("Name,Address")");
但是,如何访问这些返回的列?我无法使用 lambda 表达式访问它们,如下所示:
var test = customer.Where(cust=>cust.Name == "Mike").First();
上述情况下的“cust.Name”无法解析。它不存在于“cust”的方法/属性列表中。
我假设这里出了什么问题吗?我知道我正在与匿名类型一起工作。在这种情况下我必须创建 DTO 吗?
I am using the System.Linq.Data library provided here - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
I have the following query which works great and returns an Iqueryable
IQueryable customer =
ctx.Customers.Where(cust => true).Select("new("Name,Address")");
However, how do I access these returned columns? I cannot access them using a lambda expression as follows:
var test = customer.Where(cust=>cust.Name == "Mike").First();
"cust.Name" in the above case cannot be resolved. It does not exist in the list of methods/properties for "cust".
Am i assuming something wrong here. I understand that I am working with an anonymous type. Do I have to create a DTO in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于任何 IQueryable,您都有名为 ElementType 的属性。
您可以使用它来获取属性,如下所述
For any IQueryable you have property called ElementType.
You can use it to get the properties as explained below
尝试 foreach 循环:
Try foreach loop: