gpt4 book ai didi

c# - "OR"按需使用 Linq to Entities,我该怎么做?

转载 作者:行者123 更新时间:2023-11-30 19:11:51 26 4
gpt4 key购买 nike

我有以下按需执行“AND”的查询:

var products = // Selecting a list of products in anywhere based in a filter...

foreach (var product in products)
{
query = query.Where(p => p.Code == product.Code); // with this way, this query make no sense because this happens in any scenario, never a code can be more than one code.
}

那么,我怎样才能执行相同的查询但按需执行“或”(以便查询有意义)?

最佳答案

您可以将 IN 的传真用于 LINQ:

var productCodeList = products.Select(p => p.Code).ToList();

query = query.Where(p => productCodeList.Contains(p.Code));

基本上是说:

SELECT   *
FROM products
WHERE code IN (<list_of_codes>)

关于c# - "OR"按需使用 Linq to Entities,我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10017039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com