简体   繁体   English

如何将SQL“WHERE expr IN(query)”转换为LINQ?

[英]How to Translate SQL “WHERE expr IN (query)” into LINQ?

Basically I want to make this SQL query with linq: 基本上我想用linq进行这个SQL查询:

SELECT * 
FROM Orders 
WHERE Identifier IN (SELECT DISTINCT [Order] FROM OrderRows WHERE Quantity = '1')

This is what I have come up with: 这就是我想出的:

var q = from o in db.Orders 
     where o.Identifier in (from r in db.OrderRows 
                           where r.Quantity == 1 select r.Order).Distinct());

But the in after o.Identifier is not valid. o.Identifier无效。

What is the correct syntax for the keyword IN? 关键字IN的正确语法是什么?

    from o in db.Orders 
    where o.Identifier.Any
      (
        from r in db.OrderRows 
        where r.Quantity == 1 
        select r.Order
      ).Distinct()
    select o

Try this... 试试这个...

It seems like you want a join: 好像你想加入一个:

var q = (from o in db.Orders
        join r in db.OrderRows on o.Identifier equals r.Order
        where r.Quantity == 1
        select o).Distinct();

I'm a little late, but I made a demo! 我有点晚了,但我做了一个演示!

As other people have stated, I always use Contains: 正如其他人所说,我总是使用Contains:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ContainsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var foos = new List<Foo>
            {
                new Foo { ID = 1, FooName = "Light Side" },
                new Foo { ID = 2, FooName = "Dark Side" }
            };

            var bars = new List<Bar>
            {
                new Bar { ID = 1, BarName = "Luke", FooID = 1 },
                new Bar { ID = 2, BarName = "Han", FooID = 1 },
                new Bar { ID = 3, BarName = "Obi-Wan", FooID = 1 },
                new Bar { ID = 4, BarName = "Vader", FooID = 2 },
                new Bar { ID = 5, BarName = "Palpatine", FooID = 2 },
                new Bar { ID = 6, BarName = "Fett", FooID = 2 },
                new Bar { ID = 7, BarName = "JarJar", FooID = 3 }
            };

            var criteria = from f in foos
                           select f.ID;

            var query = from b in bars
                        where criteria.Contains(b.FooID)
                        select b;

            foreach (Bar b in query)
            {
                Console.WriteLine(b.BarName);
            }

            Console.WriteLine();
            Console.WriteLine("There should be no JarJar...");

            Console.ReadLine();
        }
    }

    public class Foo
    {
        public int ID { get; set; }
        public string FooName { get; set; }
    }

    public class Bar
    {
        public int ID { get; set; }
        public string BarName { get; set; }
        public int FooID { get; set; }
    }   
}

have you tried using something like this: 你尝试过这样的事情:

int[] inKeyword = { 5, 7, 9 };
var q = from o in db.Orders.Where(p => inKeyword.Contains(p.Identifier));

Hope it helps :) 希望能帮助到你 :)

var q = from o in db.Orders 
     where (from r in db.OrderRows 
            where r.Quantity == 1 select r.Order).Distinct().Contains(o.Identifier);

The short answer is you want to take advantage of the Contains method. 简短的回答是你想利用Contains方法。

int[] ids = { 2, 5, 6, 1 };

var a = from myRecords in context.db
where ids.Contains (myRecords.id)
select new {Id = myRecords.id};

Filtering on two sets of results works the same way, in that you can filter on any common property shared by the two sets: 对两组结果进行过滤的方式相同,因为您可以过滤这两组共享的任何公共属性:

string[] cities = { "London", "Paris", "Seattle" };
var query = dataContext.Customers.Where (c => cities.Contains (c.City));

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

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