我有这个linq查询正在进行选择,然后尝试对总数进行求和,因此在每个组键下有一个总和。值是一个字符串,我不能对字符串求和。但是当我尝试转换为int时,我收到一个错误:
LINQ to Entities无法识别方法' Int32 ToInt32(System.String)'方法,并且此方法无法转换为商店表达式。
var dataSet = entities.BL_MT_CATEGORY
.Distinct()
.Where(d => (iGeography.FirstOrDefault() == "" || iGeography.Contains(d.Geography))
&& (iRetailer.FirstOrDefault() == "" || iRetailer.Contains(d.Retailer))
&& (iCountry.FirstOrDefault() == "" || iCountry.Contains(d.Country))
&& (iAirport.FirstOrDefault() == "" || iAirport.Contains(d.Airport))
&& (iShop.FirstOrDefault() == "" || iShop.Contains(d.StoreName))
&& (iCategory.Contains(d.Category))
&& (d.Values == "Sum of EuroValue"))
.GroupBy(x => x.Category)
.Select(g => new {
C201408 = g.Sum(x => Convert.ToInt32(x.C201408))
})
.ToList();
答案 0 :(得分:1)
尝试将转化移到查询之外。
var dataSet = entities.BL_FERRERO_MT_CATEGORY
.Distinct()
.Where(d => (iGeography.FirstOrDefault() == "" || iGeography.Contains(d.Geography))
&& (iRetailer.FirstOrDefault() == "" || iRetailer.Contains(d.Retailer))
&& (iCountry.FirstOrDefault() == "" || iCountry.Contains(d.Country))
&& (iAirport.FirstOrDefault() == "" || iAirport.Contains(d.Airport))
&& (iShop.FirstOrDefault() == "" || iShop.Contains(d.StoreName))
&& (iCategory.Contains(d.Category))
&& (d.Values == "Sum of EuroValue"))
.ToList()
.GroupBy(x => x.Category)
.Select(g => new {
C201408 = g.Sum(x => Convert.ToInt32(x.C201408))});
这是使LINQ to Entities转换有效查询。之后,它将执行.Sum()
的分组和选择。
答案 1 :(得分:0)
仅获取您要求和的列,并使其成为总和。
var dataSet = entities.BL_FERRERO_MT_CATEGORY
.Distinct()
.Where(d => (iGeography.FirstOrDefault() == "" || iGeography.Contains(d.Geography))
&& (iRetailer.FirstOrDefault() == "" || iRetailer.Contains(d.Retailer))
&& (iCountry.FirstOrDefault() == "" || iCountry.Contains(d.Country))
&& (iAirport.FirstOrDefault() == "" || iAirport.Contains(d.Airport))
&& (iShop.FirstOrDefault() == "" || iShop.Contains(d.StoreName))
&& (iCategory.Contains(d.Category))
&& (d.Values == "Sum of EuroValue"))
.Select(x=>x.C201408)
.ToList()
.Sum(x=>int.Parse(x));