简体   繁体   English

多个用户指定ID的实体框架“在哪里”

[英]Entity Framework 'Where Or' for multiple user specified IDs

The user is able to enter a comma separated list of IDs in a searchbox. 用户可以在搜索框中输入以逗号分隔的ID列表。 This then needs to converted to the following SQL: 然后需要将其转换为以下SQL:

...
WHERE table.idCol = id1 OR table.idCol = id2 OR table.idCol = id3 ...

Given a string array what EF code do I need to write to produce this? 给定一个字符串数组,我需要编写哪些EF代码来生成此代码? The following doesn't work as it produces ANDs instead 以下代码无效,因为它会生成AND

foreach (string idStr in idString.Split(','))
{
    int id = int.Parse(idStr);
    query = query.Where(t => t.idCol == id);
}

Why don't you just use Contains , it will be translated as IN clause : 您为什么不只使用Contains ,它将被转换为IN子句:

var idList = idString.Split(',').Select(int.Parse).ToList();

var query = table.Where(x => idList.Contains(x.idCol));

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

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