2012-10-05 93 views
0

我尝试使用 “GROUP BY” IM MVC无法隐式转换类型 'System.Collections.Generic.List <AnonymousType#1>' 到“System.Collections.Generic.IList

这是我的功能

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

如果你明白你在说什么不是MVC,这可能会有帮助。它是Linq和实体框架。 MVC与您的问题无关,因为MVC只是提供Web请求基础架构和模板呈现的技术。 –

+0

Place显示实体ShippingByWeightRecord –

回答

3

您需要在select中创建ShippingByWeightRecord的实例。应该是:

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