无法将类型'System.Collections.Generic.List <anonymoustype#1>'隐式转换为'System.Collections.Generic.IList </anonymoustype#1>

时间:2012-10-05 05:04:44

标签: c# entity-framework linq-to-sql

我尝试使用&#34; GROUP BY&#34; 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中写它? 你有什么想法???

1 个答案:

答案 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  
                             }; 
相关问题