简体   繁体   English

具有多个条件和in子句的C#EF Linq语法查询

[英]C# EF Linq syntax query with multiple conditions and in clause

I have a linq with query syntax on entity framework. 我在实体框架上使用查询语法进行查询。 This is the sql 这是sql

Select * from x
join y on y.a = x.a
join p on p.b = x.b and x.n in (3,4)
--other joins

this is the linq 这是linq

var k = from typeX in x
join typeY in y on y.a equals x.a
join typeP in p on p.b = x.b  ???? (AND x.n in 3,4???)
//Other joins

These DOESN'T WORK 这些不起作用

join p in p on x.b equals p.b and x.n.Contains(3,4)

Even or , || 偶数 | || and && does not work and aren't recognized as keywords &&不起作用,也不被识别为关键字

Also using parenthesis ( with || or, and inside) don't work. 也不能使用括号(在||或and内)。

What am I missing? 我想念什么?

I need to do a join with multiple condition of which one is an "IN CLAUSE". 我需要进行多个条件的联接,其中一个条件是“ IN CLAUSE”。

Thanks 谢谢

Here is a DotNetFiddle with a working example of what I took from your question: 这是一个DotNetFiddle,其中有一个工作示例说明了我从您的问题中学到的内容:

You can place 3,4 into an array and add it to your LINQ query then check if the array contains xn : 您可以将3,4放入数组中并将其添加到LINQ查询中,然后检查数组是否包含xn

int[] myNumbers = new int[] {3, 4};

join p in pData on xb equals pb where myNumbers.Contains(xn)

Here is the full query (variables are slightly different than yours so it is easier for me to read them, but should be the same logic): 这是完整的查询(变量与您的变量略有不同,因此我更容易阅读它们,但逻辑应该相同):

    int[] myN = new int[] {3, 4};
    var result = from typeX in theX
        join typeY in theY on typeX.a equals typeY.a
        join typeP in theP on typeX.b equals typeP.b where myN.Contains(typeX.n)
        join typeD in theD on typeX.z equals typeD.z
        select typeX;

I'm not really familiar with query syntax for LINQ, hence I'll show how to do it using Method syntax. 我对LINQ的查询语法不是很熟悉,因此我将展示如何使用Method语法进行查询。

You want to join several tables, and after some joins, you want to join with table P. But you don't want to join all previous join results with table P, you only want to use those previous join results that that have an xn equal to 3 or 4. 您想要联接多个表,并且在进行一些联接之后,想要与表P联接。但是,您不想将所有先前的联接结果与表P联接,只想使用那些具有xn的先前联接结果。等于3或4。

If you will throw away all previous join results with xN value neither 3, nor 4, then it's no use to put them in the previous join result anyway: 如果您将所有xN值都不为3或4的先前联接结果丢弃,那么将它们放在先前联接结果中是没有用的:

var requiredNvalues = new int[] {3, 4};
var result = X
    .Where(x => requiredNvalues.Contains(x.N)  // keep only the required x elements

    // Join the remaining elements of X with Y:
    .Join(Y,                   
    x => x.A,      // from every x in X take the A
    y => y.A,      // from every y in Y take y.A

    // ResultSelector: when they match make one new object containing matching X and Y
    (x, y) => new  {X=x, Y=y})

   // join  with third table:
   .Join(Z,

   previousJoinResult => previousJoinResult.X.Id,
                            // from every item in previous join result take X.Id
   z => z.xId,              // from every z from Zcollection take Xid,

   // ResultSelector: when they match make one new object containing matching X Y Z
   (previousJoinResult, z) => new {X = previousJoinResult.X, Y = previousJoinResult.Y, Z = z}
   ... etc, other joins

Coninue joining in a similar way until you want to join P: Coninue以类似的方式加入,直到您想加入P:

   .Join(P,
   previousJoinResult => previousJoinResult.X.b, 
                             // from every X from the previous join result take b
   p => p.b                  // from every p take b

   // resultSelector: select X Y Z ... P
   (previousJoinResult, p) => new
   {
       X = previousJoinResult.X,
       Y = previousJoinResult.Y,
       Z = previousJoinResult.Z,
       ...
       P = p;
   });

Simple comme bonjour! 简单的漫画卓悦!

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

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