0

我正在尝试使用“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 写它?你有什么主意吗???

4

1 回答 1

3

您需要创建ShippingByWeightRecordin 的实例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  
                             }; 
于 2012-10-05T05:12:08.870 回答