【问题标题】:Check if id's in a comma-separated string match any id's in another string array检查逗号分隔字符串中的 id 是否与另一个字符串数组中的任何 id 匹配
【发布时间】:2020-09-12 01:05:31
【问题描述】:

我在 DB Master Setup Approval 和 Order Details 中有两个表格,我想检查任何 Master Approval Setup,此采购订单将继续依赖 CostCenter。

表主设置批准:

|-------|---------|-------------|-------------|---------------|---------------------|
|    ID |    Name |   CRG_COM_ID|  CRG_BRN_ID |ApprovalTypeId |CostCenter(string)   |
|-------|---------|-------------|-------------|---------------|---------------------|                            
|    1  | Setup1  |     1       |     1       |     1         |    "1,2,5,7"        |
|-------|---------|-------------|-------------|---------------|---------------------|
|    2  | Setup2  |     1       |     1       |     1         |     "1,3,6"         |     
|-------|---------|-------------|-------------|---------------|---------------------|

餐桌订单详情:

|-------|---------|-------------|-------------|------------------|
|    ID |    Name |   CRG_COM_ID|  CRG_BRN_ID |CostCenterID(long)|
|-------|---------|-------------|-------------|------------------|                            
|    1  | Item1   |     1       |     1       |       1          |
|-------|---------|-------------|-------------|------------------|
|    2  | Item2   |     1       |     1       |       7          |     
|-------|---------|-------------|-------------|------------------|

这是我的代码:

var orderDetails = db.OrderDetails.Where(c => c.OrderId == orderId);
var  costc = orderDetails.Select(c => c.CostCenterId.Value).ToList().ConvertAll<string>(delegate (long i) { return i.ToString(); });   
var ApprovalProcess_Count12 = db.MasterSetupApproval.Where(x =>
    x.CRG_COM_ID == order.CompanyId &&
    (x.CRG_BRN_ID == null || x.CRG_BRN_ID == order.BranchId) &&
    x.ApprovalTypeId == (int)ApprovalTypes.PO &&
    x.CostCenter.Split(',').Select(aee => aee).Any(val => costc.Contains(val))
).ToList();

我收到以下错误:

LINQ to Entities 无法识别方法 'System.String[] Split(Char[])' 方法,并且该方法无法转换为存储表达式。

输出应该是:

|-------|---------|-------------|-------------|---------------|---------------------|
|    ID |    Name |   CRG_COM_ID|  CRG_BRN_ID |ApprovalTypeId  |CostCenter(string)   |
|-------|---------|-------------|-------------|---------------|---------------------|                            
|    1  | Setup1  |     1       |     1       |     1         |    "1,2,5,7"        |
|-------|---------|-------------|-------------|---------------|---------------------|

【问题讨论】:

    标签: c# asp.net-mvc entity-framework linq linq-to-entities


    【解决方案1】:

    假设您正在使用设计不佳的数据库(如 Crowcoder 指出的那样),逗号分隔的值不应出现在数据库中,您可以参考 this 来解决问题。

    HTH!

    【讨论】:

      猜你喜欢
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多