【问题标题】:Use Contains when list having more than one field当列表具有多个字段时使用包含
【发布时间】:2014-08-20 13:10:36
【问题描述】:

我正在使用两个视图,它们都放置在不同的数据库上(在不同的服务器上)。我从两者中获取记录,我必须在 LINQ 中使用完全外连接功能(我知道 LINQ 不支持完全外连接)来获取所有记录,无论它们是否匹配。

为此,我使用以下代码 sn-p

征集 View1:

var specialRequest = (from specialReq in dc1.View1
                     select specialReq).ToList();

此视图返回多个字段,我需要所有字段。

调用 View2 并加入 View1 结果:

 var SummaryDataLeftOuter = (
    from specialReq in specialRequest
    join summary in dc.View2 on specialReq.inv_item_id equals summary.inv_item_id
    into tempspecialReq
    from summary in tempspecialReq.DefaultIfEmpty()
    select new Summary
        {
            inv_item_id = string.IsNullOrEmpty(specialReq.inv_item_id) ? "" : specialReq.inv_item_id.Contains("NO ITEM NUMBER") ? "NO PART #" : specialReq.inv_item_id,
            description = specialReq.description,
            unit_of_measure = specialReq.unit_of_measure,
            total_onorder_qty = (specialReq.TotalOrder.HasValue ? Convert.ToInt32(specialReq.TotalOrder) : 0) + (summary == null ? 0 : summary.TotalOrder.HasValue ? Convert.ToInt32(summary.TotalOrder) : 0),
            open_order_qty = (specialReq.TotalOpen.HasValue ? Convert.ToInt32(specialReq.TotalOpen) : 0) + (summary == null ? 0 : summary.OpenOrder.HasValue ? Convert.ToInt32(summary.OpenOrder) : 0),
            picked_qty = summary == null ? 0 : summary.PickedQty.HasValue ? Convert.ToInt32(summary.PickedQty) : 0,
            qty_shipped = summary == null ? 0 : summary.qty_shipped.HasValue ? Convert.ToInt32(summary.qty_shipped) : 0,
            EnableLink = string.IsNullOrEmpty(specialReq.inv_item_id) ? false : specialReq.inv_item_id.Contains("NO ITEM NUMBER") ? false : true
        }
).ToList();

更改表格顺序后再次使用 Join:

var SummaryDataRightOuter = (
    from summary in dc.View2
    join specialReq in specialRequest on summary.inv_item_id equals specialReq.inv_item_id
    into tempsummary
    from specialReq in tempsummary.DefaultIfEmpty()
    select new Summary
        {
            inv_item_id = string.IsNullOrEmpty(summary.inv_item_id) ? "" : summary.inv_item_id.Contains("NO ITEM NUMBER") ? "NO PART #" : summary.inv_item_id,
            description = summary.descr60,
            unit_of_measure = summary.unit_of_measure,
            total_onorder_qty = specialReq == null ? 0 : (specialReq.TotalOrder.HasValue ? Convert.ToInt32(specialReq.TotalOrder) : 0) + (summary == null ? 0 : summary.TotalOrder.HasValue ? Convert.ToInt32(summary.TotalOrder) : 0),
            open_order_qty = specialReq == null ? 0 : (specialReq.TotalOpen.HasValue ? Convert.ToInt32(specialReq.TotalOpen) : 0) + (summary == null ? 0 : summary.OpenOrder.HasValue ? Convert.ToInt32(summary.OpenOrder) : 0),
            picked_qty = summary == null ? 0 : summary.PickedQty.HasValue ? Convert.ToInt32(summary.PickedQty) : 0,
            qty_shipped = summary == null ? 0 : summary.qty_shipped.HasValue ? Convert.ToInt32(summary.qty_shipped) : 0,
            EnableLink = specialReq == null ? false : string.IsNullOrEmpty(specialReq.inv_item_id) ? false : specialReq.inv_item_id.Contains("NO ITEM NUMBER") ? false : true
        }
);

现在在第二次通话中,我收到以下异常:

  Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator

我已经在网上搜索了很多,得到了很多解决方案,他们都说要使用 Contains。但是他们都给出了第一个列表的例子,即 specialRequest 只包含一个列,例如One example you can check here。但我需要所有列。

问题:

当列表包含多个字段时如何使用 Contains()。

【问题讨论】:

    标签: c# sql linq linq-to-sql


    【解决方案1】:

    在我的问题中,您看到我直接在 join 中使用 view2,而没有在任何列表中分配它。这是问题的主要原因。

    解决方案:

    首先将视图结果分配到列表中,然后使用此列表进行进一步处理。

    【讨论】:

      猜你喜欢
      • 2013-10-15
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多