从两个单独的数据上下文中加入列表数据

时间:2017-10-13 00:37:35

标签: c# asp.net-mvc linq

我一直在寻找这个问题的解决方案,这就是我到目前为止所做的:

var ProductInfo = (from p in twd.Products
                               orderby p.PC
                               where p.DELMARK == active
                               select p).AsEnumerable();
        var BuyersData =
            (from x in db.MinimumProductInfo
             where x != null
             orderby x.ItemCode, x.Region
             let pnote =
                 (from pn in db.ProductNotes
                  where pn != null
                  where x.MinimumProductInfoID == pn.MinimumProductInfoID
                      && pn.NoteTypeFlag == "p"
                  orderby pn.NoteDate descending
                  select pn).FirstOrDefault()
             let cnote =
                 (from c in db.ProductNotes
                  where c != null
                  where x.MinimumProductInfoID == c.MinimumProductInfoID
                       && c.NoteTypeFlag == "c"
                  orderby c.NoteDate descending
                  select c).FirstOrDefault()
             let product =
                 (from p in ProductInfo
                  where x.ItemCode == p.PC
                  select p).FirstOrDefault()
             select new ProductInfoWithNoteList
             {
                 MinimumProductInfoID = x.MinimumProductInfoID,
                 ItemCode = x.ItemCode,
                 EquivCode = x.EquivCode,
                 Description = product.PDESC,
                 MinimumOnHandQuantity = x.MinimumOnHandQuantity,
                 MaximumOHandQuantity = x.MaximumOHandQuantity,
                 MinimumOrderQuantity = x.MinimumOrderQuantity,
                 LeadTimeInWeeks = x.LeadTimeInWeeks,
                 Region = x.Region,
                 Comment = cnote.ItemNote,
                 PermanentNote = pnote.ItemNote
             }).ToArray();

看起来不对,但我收到了错误,

  

'指定的LINQ表达式包含对查询的引用   与不同的背景相关联。'

此代码应该执行的操作是使用twd datacontext从第一个表中提取所有活动产品代码,然后在db.MinimumProductInfo表中使用该数据库中的数据。他们有两个独立的数据上下文的原因是它们是完全不同的数据库,第一个是我们的ERP,第二个是我们在内部构建的数据库。

我错过了什么?我知道可以通过分离两个datacontexts然后将它们添加到一起来实现这一点,因为我已经看到它完成了单个实例但我找不到如何使用列表数据。

1 个答案:

答案 0 :(得分:0)

而不是:

let product =
             (from p in ProductInfo
              where x.ItemCode == p.PC
              select p).FirstOrDefault()
         select new ProductInfoWithNoteList
         {
             MinimumProductInfoID = x.MinimumProductInfoID,
             ItemCode = x.ItemCode,
             EquivCode = x.EquivCode,
             Description = product.PDESC,
             MinimumOnHandQuantity = x.MinimumOnHandQuantity,
             MaximumOHandQuantity = x.MaximumOHandQuantity,
             MinimumOrderQuantity = x.MinimumOrderQuantity,
             LeadTimeInWeeks = x.LeadTimeInWeeks,
             Region = x.Region,
             Comment = cnote.ItemNote,
             PermanentNote = pnote.ItemNote
         }).ToArray();

尝试删除let product子句并且不填充与ProductInfo相关联的属性,因为我们之后会这样做(请参阅我已注释掉Description属性):

select new ProductInfoWithNoteList
{
    MinimumProductInfoID = x.MinimumProductInfoID,
    ItemCode = x.ItemCode,
    EquivCode = x.EquivCode,
    //Description = product.PDESC,
    MinimumOnHandQuantity = x.MinimumOnHandQuantity,
    MaximumOHandQuantity = x.MaximumOHandQuantity,
    MinimumOrderQuantity = x.MinimumOrderQuantity,
    LeadTimeInWeeks = x.LeadTimeInWeeks,
    Region = x.Region,
    Comment = cnote.ItemNote,
    PermanentNote = pnote.ItemNote
}).ToArray();

现在你的BuyersDataProductInfo已经在内存中,设置Description属性或BuyersData中的所有项目:

foreach(var thisBuyerData in BuyersData)
{
    var thisPi = ProductInfo.SingleOrDefault(x => x.PC == thisBuyerData.ItemCode);

    thisBuyerData.Description = thisPi?.PDESC;
}
相关问题