我尝试使用" GROUP BY" im MVC
这是我的功能
private readonly ICacheManager _cacheManager;
public virtual IList<ShippingByWeightRecord> GetAll()
{
string key = SHIPPINGBYWEIGHT_ALL_KEY;
return _cacheManager.Get(key, () =>
{
var query = from sbw in _sbwRepository.Table
group sbw by new
{
sbw.ShippingMethodId,
sbw.From,
sbw.To,
sbw.ShippingChargeAmount,
sbw.RegionId,
sbw.ItemRangeId
}
into grouping
select new { grouping.Key,
Id = grouping.Max(sbw => sbw.Id)
};
var records = query.ToList();
return records;
});
}
但是有错误。怎么做?? 这是我的sql命令。
SELECT MIN(Id) AS id, ShippingMethodId, [From], [To], ShippingChargeAmount, RegionId, ItemRangeId
FROM ShippingByWeight
GROUP BY ShippingMethodId, [From], [To], ShippingChargeAmount, RegionId, ItemRangeId
我想在MVC中写它? 你有什么想法???
答案 0 :(得分:3)
您需要在ShippingByWeightRecord
中创建select
的实例。应该是:
var query = from sbw in _sbwRepository.Table
group sbw by new
{
sbw.ShippingMethodId,
sbw.From,
sbw.To,
sbw.ShippingChargeAmount,
sbw.RegionId,
sbw.ItemRangeId
}
into grouping
select new ShippingByWeightRecord {
Id = grouping.Max(sbw => sbw.Id),
ShippingMethodId = grouping.Key.ShippingMethodId,
From = grouping.Key.From,
To = grouping.Key.To,
ShippingChargeAmount = grouping.Key.ShippingChargeAmount,
RegionId = grouping.Key.RegionId,
ItemRangeId = grouping.Key.ItemRangeId
};