CREATE TABLE [MyNames]
(
[ID] INT IDENTITY PRIMARY KEY,
[Name] NVARCHAR(255) NULL
)
INSERT INTO [MyNames] VALUES ('John')
INSERT INTO [MyNames] VALUES ('Jane')
INSERT INTO [MyNames] VALUES ('Peter')
INSERT INTO [MyNames] VALUES ('Montgomery')
INSERT INTO [MyNames] VALUES ('Sarah')
基于上面的(假设的)SQL模式和数据,我想使用Linq to SQL来获取名称为数组的值中的的所有结果。
string[] names = {"John", "Cassandra", "Sarah"};
var results = (from n in db.Names
where n.Name **in names**
select n).ToList();
结果应包括
John
和Sarah
。然后,利用这些信息,我可以添加需要添加的条目,在本例中为Cassandra
。我不想加载所有名称,因为列表可能会非常长。
最佳答案
您可以使用names.Contains()
:
string[] names = {"John", "Cassandra", "Sarah"};
var results = (from n in db.Names
where names.Contains(n.Name)
select n).ToList();
关于c# - Linq到SQL : WHERE IN statement,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18235409/