在linq中对字符串列求和

时间:2013-12-04 17:41:19

标签: c# linq linq-to-sql

我有这样的查询:

var q = from i in dbconnect.tblMaterialTenderGroups
   join b in dbconnect.tblMaterials on i.materialId equals b.materialId
   join f in dbconnect.tblFactoryRequests on b.requestId equals f.requestId
   where i.MaterialGroupId == materialGroupId && f.propertyFactoryCenteralId.Contains(facName)
   select b;

我确信我的“q”有记录,但是当我执行此查询时:

int? sum = q.Sum(g => Int32.Parse(g.requestAmount));

我收到了这个错误:

  

{System.InvalidOperationException:无法翻译表达式    表(tblMaterialTenderGroup).Join(表(tblMaterial),i => i.materialId,b =>
  转换(b.materialId),(i,b)=> new<> f_AnonymousType'2(i = i,b = b))。加入(表(tblFactoryRequest),<> h_ TransparentIdentifier5 =><> h _TransparentIdentifier5.b .requestId,f =>转换(f.requestId),(<> h_ TransparentIdentifier5,f)=> new   <>˚F _AnonymousType1'2(小于;> H_ TransparentIdentifier5        =<> h _TransparentIdentifier5,f = f))。其中(<> h_ TransparentIdentifier6 =>   ((<> h _TransparentIdentifier6。<> h_ TransparentIdentifier5.i.MaterialGroupId ==   调用(value(System.Func 1[System.Nullable 1 [System.Int32]])))AndAlso   <> H _TransparentIdentifier6.f.propertyFactoryCenteralId.Contains(调用(值(System.Func`1   [System.String])))))。选择(<> h_ TransparentIdentifier6 =><> h _TransparentIdentifier6。   <> h__TransparentIdentifier5.b).Sum(g => Parse(g.requestAmount))到SQL中,无法将其视为本地表达式。

1 个答案:

答案 0 :(得分:6)

Int32.Parse无法转换为SQL。请改用Convert.ToInt32

 int? sum = q.Sum(g => Convert.ToInt32(g.requestAmount));