相当于 SQL IN 运算符的 linq 是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2334327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:29:30  来源:igfitidea点击:

What is the linq equivalent to the SQL IN operator

sqllinqcontains

提问by Luca Romagnoli

With linq I have to check if a value of a row is present in an array.
The equivalent of the sql query:

使用 linq,我必须检查数组中是否存在行的值。
相当于 sql 查询:

WHERE ID IN (2,3,4,5)

How can I do it?

我该怎么做?

采纳答案by David Morton

.Contains

.包含

var resultset = from x in collection where new[] {2,3,4,5}.Contains(x) select x

Of course, with your simple problem, you could have something like:

当然,对于您的简单问题,您可能会遇到以下问题:

var resultset = from x in collection where x >= 2 && x <= 5 select x

回答by Lachlan Roche

Perform the equivalent of an SQL IN with IEnumerable.Contains().

使用IEnumerable.Contains()执行等效的 SQL IN 。

var idlist = new int[] { 2, 3, 4, 5 };

var result = from x in source
          where idlist.Contains(x.Id)
          select x;

回答by Justin Niessner

db.SomeTable.Where(x => new[] {2,3,4,5}.Contains(x));

or

或者

from x in db.SomeTable
where new[] {2,3,4,5}.Contains(x)

回答by atreeon

Intersect and Except are a little more concise and will probably be a bit faster too.

Intersect 和 except 更简洁一点,也可能会更快一点。

IN

collection.Intersect(new[] {2,3,4,5});

NOT IN

不在

collection.Except(new[] {2,3,4,5});

or

或者

Method syntax for IN

IN 的方法语法

collection.Where(x => new[] {2,3,4,5}.Contains(x));

and NOT IN

并且不在

collection.Where(x => !(new[] {2,3,4,5}.Contains(x)));

回答by Nathan Taylor

An IEnumerable<T>.Contains(T)statement should do what you're looking for.

一个IEnumerable<T>.Contains(T)语句应该做你正在寻找的。

回答by Kamal

A very basic example using .Contains()

使用 .Contains() 的一个非常基本的例子

List<int> list = new List<int>();
for (int k = 1; k < 10; k++)
{
    list.Add(k);
}

int[] conditionList = new int[]{2,3,4};

var a = (from test in list
         where conditionList.Contains(test)
         select test);

回答by AndreyAkinshin

You can write help-method:

您可以编写帮助方法:

    public bool Contains(int x, params int[] set) {
        return set.Contains(x);
    }

and use short code:

并使用短代码:

    var resultset = from x in collection
                    where Contains(x, 2, 3, 4, 5)
                    select x;

回答by user12063853

The above situations work when the Containsfunction is used against primitives, but what if you are dealing with objects (e.g. myListOrArrayOfObjs.Contains(efObj))?

Contains函数用于原始类型时,上述情况有效,但是如果您正在处理对象(例如myListOrArrayOfObjs.Contains(efObj))呢?

I found a solution! Convert your efObjinto a string, thats separated by _for each field (you can almost think of it as a CSV representation of your obj)

我找到了解决办法!将您的efObj转换为string,即_每个字段的分隔符(您几乎可以将其视为 obj 的 CSV 表示)

An example of such may look like this:

此类示例可能如下所示:

     var reqAssetsDataStringRep = new List<string>();

        foreach (var ra in onDemandQueueJobRequest.RequestedAssets)
        {
            reqAssetsDataStringRep.Add(ra.RequestedAssetId + "_" + ra.ImageId);
        }

        var requestedAssets = await (from reqAsset in DbContext.RequestedAssets
                                     join image in DbContext.Images on reqAsset.ImageId equals image.Id
                                     where reqAssetsDataStringRep.Contains(reqAsset.Id + "_" + image.Id)
                                     select reqAsset
                                           ).ToListAsync();

回答by Shahid

Following is a generic extension method that can be used to search a value within a list of values:

以下是可用于在值列表中搜索值的通用扩展方法:

    public static bool In<T>(this T searchValue, params T[] valuesToSearch)
    {
        if (valuesToSearch == null)
            return false;
        for (int i = 0; i < valuesToSearch.Length; i++)
            if (searchValue.Equals(valuesToSearch[i]))
                return true;

        return false;
    }

This can be used as:

这可以用作:

int i = 5;
i.In(45, 44, 5, 234); // Returns true

string s = "test";
s.In("aa", "b", "c"); // Returns false

This is handy in conditional statements.

这在条件语句中很方便。