gpt4 book ai didi

如果过滤器参数为空,则对实体进行 linq,然后选择所有实体?

转载 作者:行者123 更新时间:2023-12-04 02:49:37 26 4
gpt4 key购买 nike

我使用 ID 过滤实体,但如果 ID 为空,则我想返回所有实体。所以在下面的代码中,如果 competitorID 为 null id,则返回所有简短的竞争对手。

var competitors =
NeptuneUnitOfWork.Briefs.FindWhere(b => b.ID == briefID)
.Select(b => b.BriefCompetitors.Where(b=>b.ID == competitorID)).ToList();

最佳答案

通常你这样做:

IQueryable<Brief> briefs = NeptuneUnitOfWork.Briefs.Where(b => b.ID == briefID);

if (competitorID != null)
{
competitors = briefs.Select(b => b.BriefCompetitors.Where(b=>b.ID == competitorID));
}
else
{
competitors = briefs.Select(b => b.BriefCompetitors);
}

从技术上讲,您可以将问题留给 SQL Server:

// Note the || clause
competitors = briefs.Where(b => b.ID == briefID)
.Select(b => b.BriefCompetitors.Where(b=>b.ID == competitorID || competitorID == null));

关于如果过滤器参数为空,则对实体进行 linq,然后选择所有实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18033974/

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