linq相当于操作符中的SQL是什么

时间:2022-08-09 15:46:27

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?

我怎么能做什么?

8 个解决方案

#1


45  

.Contains

.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

#2


25  

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

使用IEnumerable.Contains()执行与SQL相同的操作。

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

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

#3


9  

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)

#4


8  

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

交叉,除了稍微简洁一点,而且可能会更快一点。

IN

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

NOT IN

不是在

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

or

Method syntax for IN

语法的方法

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

and NOT IN

而不是在

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

#5


3  

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

IEnumerable . contains (T)语句应该符合您的要求。

#6


2  

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);

#7


1  

You can write help-method:

你可以写help方法:

    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;

#8


0  

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.

这在条件语句中很有用。

#1


45  

.Contains

.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

#2


25  

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

使用IEnumerable.Contains()执行与SQL相同的操作。

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

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

#3


9  

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)

#4


8  

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

交叉,除了稍微简洁一点,而且可能会更快一点。

IN

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

NOT IN

不是在

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

or

Method syntax for IN

语法的方法

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

and NOT IN

而不是在

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

#5


3  

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

IEnumerable . contains (T)语句应该符合您的要求。

#6


2  

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);

#7


1  

You can write help-method:

你可以写help方法:

    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;

#8


0  

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.

这在条件语句中很有用。