[英]Linq Include and Where Clause
我有以下linq聲明:
var result = _context.Settings
.Include(x => x.Files)
.Where(y => y.Files.Where(f => f.SettingFile == true))
.Select(z => z.Deleted == null)
.ToList();
我想要做的是從表中獲取所有設置。 我還想包含files-table並獲取具有SettingFile true的所有文件。 但我一直得到以下錯誤:
不能將IEnumerable(UploadedFile)類型轉換為bool。
這是我的UploadedFile模型:
[Required]
public string FileName { get; set; }
[Required]
public string FileExtension { get; set; }
[Required]
public byte[] FileContent { get; set; }
public Guid? FormAnswerId { get; set; }
public Guid? LicenseHolderId { get; set; }
public Guid? CaseId { get; set; }
public Guid? ErrandId { get; set; }
public bool SettingFile { get; set; }
這是我的設置模型:
public class Setting : ModelBase
{
public string Key { get; set; }
public string DisplayName { get; set; }
public string DisplayText { get; set; }
public string DisplayTab { get; set; }
public string Value { get; set; }
public string Type { get; set; }
public virtual ICollection<UploadedFile> Files { get; set; }
}
模型庫:
public abstract class ModelBase : EntityBase
{
public DateTime? Deleted {get; set;}
}
我的查詢錯誤是什么?
我不太明白你想要完成什么,但我可以解釋你的問題。
Where()
子句是Func<T, bool>
。 傳入T
的實例,它返回一個bool
值。
在這一行.Where(y => y.Files.Where(f => f.SettingFile == true))
,你將返回一個IEnumerable<T>
,它應該返回一個bool
。 這就是造成錯誤的原因。 您可以通過將y.Files.Where(f => f.SettingFile == true)
更改為y.Files.Any(f => f.SettingFile)
或y.Files.All(f => f.SettingFile)
。 但是,我不認為這是你想要完成的。
編輯:由於您嘗試獲取具有SettingFile == true
所有設置,因此請執行以下操作:
_context.Settings
.Include(x => x.Files.Where(f => f.SettingFile == true))
.Select(z => z.Deleted == null)
.ToList();
編輯:在評論中進一步溝通后,你說你想要一個Setting
集合。 所以你只需要刪除.Select(x => z.Deleted == null)
,你應該是金色的。
_context.Settings
.Include(x => x.Files.Where(f => f.SettingFile == true))
.ToList();
我不知道您的用例,但建議添加另一個Where
條件以排除任何沒有SettingFile
為true
Files
Settings
。
_context.Settings
.Include(x => x.Files.Where(f => f.SettingFile == true))
.Where(x => x.Files.Any(f => f.SettingFile == true))
.ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.