2012-10-05 111 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 
          }; 
相關問題