gpt4 book ai didi

C# Entity Framework Linq : Select Where If/Else

转载 作者:行者123 更新时间:2023-12-01 22:19:25 27 4
gpt4 key购买 nike

是否可以使用一系列嵌套的 if 语句执行 .Where

这就是我想做的:

public class Repository {
public async Task<IQueryable<Order>> orders (int? accountId, int? userId) {
IQueryable<Order> orders;

orders = _context.Orders
.Where(o =>
{
int filterCount = 0;
int filterCriteriaMet = 0;

// if the accountId param is not null
// increment the filter counter
// and check if this order's accountId == param
// if so, increment filterCriteriaMet counter
if (accountId != null)
{
filterCount++;

if (o.AccountId == accountId)
{
filterCriteriaMet++;
}
}

// if the userId param is not null
// increment the filter counter
// and check if this order's userId == param
// if so, increment filterCriteriaMet counter
if (userId != null)
{
filterCount++;

if (o.UserId == userId) {
filterCriteriaMet++;
}
}

return filterCount == filterCriteriaMet;
});

return orders;
}
}

但这给了我以下错误:

A lambda expression with a statement body cannot be converted to an expression tree

最佳答案

当然!

.Where(o => AccountId != null && o.AccountId == accountId || (UserId != null && o.UserId == UserId));

关于C# Entity Framework Linq : Select Where If/Else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55201860/

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