如何从匿名类型列表中迭代?
我有这个实体查询的 linq:
c.CreateQuery<T_Hotels>("T_Hotels").Where(item => item.Id > 0).GroupBy(item => new { item.Location, item.Name }).Select(group => new { group.Key, Total = group.Sum(item => item.Id) })
我想在类 Helper 的方法中实现所有内容,如何声明 GroupBy 和 Select 的表达式,什么返回类型?
public IQueryable<???> GetHotelsGroupBy(Expression<Func<T_Hotels, bool>> pWhere,
??? pGroupBy, ??? pSelect)
{
return c.CreateQuery<T_Hotels>("T_Hotels").Where(pWhere).GroupBy(pGroupBy).Select(pSelect);
}
对不起我的英语。 里诺
i have this linq to entities query:
c.CreateQuery<T_Hotels>("T_Hotels").Where(item => item.Id > 0).GroupBy(item => new { item.Location, item.Name }).Select(group => new { group.Key, Total = group.Sum(item => item.Id) })
I want to implement everything in a method of a class Helper, how declare the expressions of GroupBy and Select, what return type?
public IQueryable<???> GetHotelsGroupBy(Expression<Func<T_Hotels, bool>> pWhere,
??? pGroupBy, ??? pSelect)
{
return c.CreateQuery<T_Hotels>("T_Hotels").Where(pWhere).GroupBy(pGroupBy).Select(pSelect);
}
Sorry for my English language.
Rino
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
理想情况下,匿名类型不应暴露在使用它们的类型之外,除非您乐意使用反射与它们交谈。但是,您可以返回非泛型
IEnumerable
或IList
。有一个叫做“的东西example”,可以使匿名类型恢复到您所期望的,但这是非常脆弱的。声明一个自定义类型来表示此 API 上的数据会更好(而且工作量更少)。这样你就不会每一步都与系统作斗争。
Anonymous types should ideally not be exposed outside of the type that uses them, unless you are happy to use reflection to talk to them. However, you could return the non-generic
IEnumerable
orIList
.There is something called "cast by example" that can work to get anon-types back to what you expect, but that is very brittle. It would be better (and less work) to declare a custom type to represent the data over this API. Then you aren't fighting the system at every step.