简体   繁体   English

LINQ有条件地使用不同的字段来加入

[英]LINQ Conditionally use different fields to join

I have this situation: 我有这种情况:

Table A 
Field A  Field B
   d       1 
   d       2
   d       3
Table B 
Field A  Field B
   d       1
   d       2
   d       3
   d       4

I want the "4" in Table B to match to the "3" in Table A. 我希望表B中的“ 4”与表A中的“ 3”匹配。
Here's my code currently: 这是我目前的代码:

short maxFieldB = DatabaseContext.TableA
    .OrderByDescending(x => x.FieldB)
    .Select(s => s.FieldB)
    .First();

IQueryable<FieldXType> fieldX = (from tableb in DatabaseContext.TableB
    join tablea in DatabaseContext.TableA on new 
    {
        a = tablea.FieldA,
        b = tablea.FieldB
    }
    equals new
    {
        a = tableb.FieldA,
        b = (tableb.FieldB > maxFieldB ? maxFieldB : tableb.FieldB)
    })
    .ToList();

When I run this, I get an exception "Argument types do not match". 运行此命令时,出现异常“参数类型不匹配”。 The stack trace seems to point at my statement that includes the ternary operator. 堆栈跟踪似乎指向我的包含三元运算符的语句。

(at System.Linq.Expressions.Expression.Condition(Expression test, 
Expression ifTrue, Expression ifFalse))  

I thought I was doing the equivalent to a T-SQL CASE statement. 我以为我在做与T-SQL CASE语句等效的操作。 Am I going about this wrong? 我要解决这个错误吗? If so, how do I make the "4" on "Table B" match to the "3" on "Table A"? 如果是这样,如何使“表B”上的“ 4”与“表A”上的“ 3”匹配? Does it have anything to do with my switching from Lambda syntax to query syntax in the middle? 与我从Lambda语法切换到中间的查询语法有什么关系吗?

Thanks in advance. 提前致谢。

Alright, as far as i understood your question, You have two tables and you want to join them on multiple conditions. 好吧,据我了解的问题,您有两个表,并且您想在多个条件下将它们加入。 The error is correct "Argument types do not match". 错误是正确的"Argument types do not match". Here are few issues with the code: 这是代码的几个问题:

IQueryable<FieldXType> fieldX = (from tableb in DatabaseContext.TableB
        join tablea in DatabaseContext.TableA on new Eaxmple1 //add a type name that contains defination for a and b
        {
            a = tablea.FieldA,
            b = tablea.FieldB//This is of some type say FieldB(as defined in your Model Eaxmple1)
        }
        equals new Eaxmple1 
        {
            a = tableb.FieldA,
            b = (tableb.FieldB > maxFieldB ? maxFieldB : tableb.FieldB) //and this maxFieldB is of another type 'short' as you defined above.
        } select tablea).ToList();

Also your query is missing select statement which cases the issue. 此外,您的查询缺少select语句,从而解决了该问题。

Here are some quick fixes that might be helpful: 以下是一些可能有用的快速修复:

Create a new model that contains the definition for a and b if not created already and make sure that their type should same as that of your table. 创建一个新模型,其中包含ab的定义(如果尚未创建),并确保它们的类型与表的类型相同。 eg 例如

public class Eaxmple1
{
public field a {get;set;}
public field b {get;set;}
}

I can help better if you can provide the more details of the tables. 如果您可以提供表格的更多详细信息,我们将为您提供更好的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM