簡體   English   中英

在 C# Linq 查詢中使用 where

[英]Using where in C# Linq query

正如您在下面分享的代碼塊中所見,我使用 where 命令。 如果這里的jopId為0,我在這里要做的是列出表中的所有數據。我該怎么做。

var studentJobCategoryList = await this._studentJobCategoryRepository.Query()
                        .Include(f => f.Student)
                        .Include(f => f.JobCategory)
                        .Where(f => f.JobCategory.JobID == request.JobID)
                        .ToListAsync();

代碼2(如this answer ):

 var studentJobCategoryList = await this._studentJobCategoryRepository.Query()
                            .Include(f => f.Student)
                            .Include(f => f.JobCategory)
                            .Where(f => (f.JobCategory.JobID ?? 0) == 0 || f.JobCategory.JobID == request.JobID)
                            .ToListAsync();

當我像第二部分一樣編輯我的代碼時

Severity Code Description Project File Line Suppression State
Error CS0019 Operator '??' cannot be applied to operands of type 'int' and 'int' Business D:\TBF\Tbf.AkademiV2.Backends\Tbf.AkademiV2.Backend\Business\Handlers\Admin\StudentJobCategory\Queries\GetStudentJobCategoriesInfoBy ActiveJobI

我得到一個錯誤。

如果您想獲得所有“jobid”為空,請使用以下代碼

var studentJobCategoryList = await this._studentJobCategoryRepository.Query()
                        .Include(f => f.Student)
                        .Include(f => f.JobCategory)
                        .Where(f => !f.JobCategory.JobID.HasValue || f.JobCategory.JobID == request.JobID)
                        .ToListAsync();

但是如果您想在請求“JobID”為 0 時獲取所有數據,請使用以下代碼

var studentJobCategoryList = await this._studentJobCategoryRepository.Query()
                        .Include(f => f.Student)
                        .Include(f => f.JobCategory)
                        .Where(f => request.JobID == 0 || f.JobCategory.JobID == request.JobID)
                        .ToListAsync();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM