【问题标题】:Sum a string column in linq在linq中对字符串列求和
【发布时间】:2013-12-05 02:16:14
【问题描述】:

我有这样的疑问:

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 =>
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.Func1[System.Nullable1[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)) 到 SQL 中,无法将其视为本地表达式。

【问题讨论】:

    标签: c# linq linq-to-sql


    【解决方案1】:

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

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

    【讨论】:

    • @Spad LINQ to SQL 无法将任何常规 .NET 方法转换为 SQL 代码。您只能使用一组方法 - 例如String.StartsWithCollection.Contains。与将字符串转换为整数相同
    • 我有这个代码:_payLog.Where(x => x.IsSucceed).Sum(x => Convert.ToInt32(x.Price)) 和这个错误:LINQ to Entities 无法识别方法 'Int32 ToInt32(System.String)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多