LINQ Lambda Join错误-无法从使用情况推断出 [英] LINQ Lambda Join Error - cannot be inferred from the usage

查看:183
本文介绍了LINQ Lambda Join错误-无法从使用情况推断出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在加入两个DbSet时遇到麻烦,并继续收到无法推断的错误".我努力寻找一个解决方案,所以我想我会分享我的简单答案.乔恩·斯凯特(Jon Skeet)等人发表了好几篇好文章,但大多数答案都在我头上.

I had troubles joining two DbSets and continued to receive the "cannot be inferred error". I struggled to find a solution so I thought I would share my simple answer. There are several great posts from Jon Skeet and others but most of the answers were over my head.

以下是引起我麻烦的代码:

Here is the code that was causing me trouble:

using(var db = new SomeDataContext())
    {
    db.DemandData
        .Where(demand=> demand.ID == SearchID)
        .Join(db.CUST_ORDER_LINE,
            supply=> new { supply.LINE, supply.SALES_ORDER_ID },
            demand=> new { demand.LINE_NO, demand.CUST_ORDER_ID },
            (supply, demand) => new { custOrderLineReturn = demand })
        .Select(s => s.custOrderLineReturn )
        .ToList();
    }

推荐答案

我已经进行了很多次此联接,以至于直到找到贾斯汀·尼斯纳(Justin Niessner)的帖子匿名类型(以及它们的类型)中的属性名称必须完全匹配." 那把我引到了这段代码:

I have done this join so many times that I could not figure out why it would not work until I found a post from Justin Niessner here that says "The names of the properties in the anonymous types (as well as their types) must match exactly." That lead me to this code:

using(var db = new SomeDataContext())
    {
  return db.DemandData
        .Where(demand=> demand.ID == SearchID)
        .Join(db.CUST_ORDER_LINE,
            supply=> new { LINE_NO = supply.LINE, CUST_ORDER_ID = supply.SALES_ORDER_ID },
            demand=> new { demand.LINE_NO, demand.CUST_ORDER_ID },
            (supply, demand) => new { custOrderLineReturn = demand })
        .Select(s => s.custOrderLineReturn )
        .ToList();
    }

在第六行中,添加了与第七行中的字段名称匹配的变量 LINE_NO = CUST_ORDER_ID = .

In the sixth line I added variables LINE_NO = and CUST_ORDER_ID = that matched the field names in line seven.

这篇关于LINQ Lambda Join错误-无法从使用情况推断出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆