Sum a string column in linq

c#, linq, linq-to-sql

Solution

`Int32.Parse` cannot be translated into SQL. Use `Convert.ToInt32` instead:

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

Problem

I have a query like that : ``` 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; ``` I am sure that my "q" has a record but when i execute this query: ``` int? sum = q.Sum(g => Int32.Parse(g.requestAmount)); ``` I got this Error: { System.InvalidOperationException: Could not translate expression Table(tblMaterialTenderGroup).Join(Table(tblMaterial), i => i.materialId, b => Convert(b.materialId), (i, b) => new <>f_AnonymousType'2(i = i, b = b)).Join(Table(tblFactoryRequest), <>h_TransparentIdentifier5 => <>h_TransparentIdentifier5.b.requestId, f => Convert(f.requestId), (<>h_TransparentIdentifier5, f) => new <>f_AnonymousType1'2(<>h_TransparentIdentifier5 = <>h_TransparentIdentifier5, f = f)).Where(<>h_TransparentIdentifier6 => ((<>h_TransparentIdentifier6.<>h_TransparentIdentifier5.i.MaterialGroupId == Invoke(value(System.Func`1[System.Nullable`1[System.Int32]]))) AndAlso <>h_TransparentIdentifier6.f.propertyFactoryCenteralId.Contains(Invoke(value(System.Func`1 [System.String]))))).Select(<>h_TransparentIdentifier6 => <>h_TransparentIdentifier6. <>h__TransparentIdentifier5.b).Sum(g => Parse(g.requestAmount)) into SQL and could not treat it as a local expression.

Original source