[英]MVC3 query clause custom query
First Table +--------+------------+-------+ | type | variety | price | +--------+------------+-------+ | apple | gala | 2.79 | | apple | fuji | 0.24 | | apple | limbertwig | 2.87 | | orange | valencia | 3.59 | | orange | navel | 9.36 | | pear | bradford | 6.05 | | pear | bartlett | 2.14 | | cherry | bing | 2.55 | | cherry | chelan | 6.33 | +--------+------------+-------+
Second Table +--------+----------+ | type | minprice | +--------+----------+ | apple | 0.24 | | cherry | 2.55 | | orange | 3.59 | | pear | 2.14 | +--------+----------+
select type, min(price) as minprice
from fruits
group by type;
第一个表是我拥有的数据的示例,第二个表是我想要从第一个表中获取的数据。
我正在使用GenericRepository/UnitOfwork
从存储库获取数据。
repository.fruitRepository.Get().GroupBy(m => m.type);
但是我只能获取类型字段,但我想获取更多字段。
在groupby之前需要使用select子句吗? 如果是,如何选择更多字段?
GroupBy方法返回更多数据,但作为可枚举返回...您可能可以在GroupBy之后使用Select将想要的内容从其中提取...
repository.fruitRepository.Get()
.GroupBy(m => m.type)
.Select(m => new { type = m.Key, minPrice = m.Min(f => f.Price) });
或者,如果您更喜欢LINQ语句:
var result = from x in repository.fruitRepository.Get()
group x by x.type into typeGroup
select new
{
type = typeGroup.Key,
minPrice = typeGroup.Min(item => item.Price)
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.