gpt4 book ai didi

c# - Linq 包含和 Where 子句

转载 作者:行者123 更新时间:2023-11-30 21:42:58 25 4
gpt4 key购买 nike

我有以下 linq 语句:

var result = _context.Settings
.Include(x => x.Files)
.Where(y => y.Files.Where(f => f.SettingFile == true))
.Select(z => z.Deleted == null)
.ToList();

我想做的是从表中获取所有设置。我还想包含文件表并获取所有具有 SettingFile true 的文件。但我一直收到以下错误:

Cannot implicity convert type IEnumerable(UploadedFile) to bool.

这是我上传的文件模型:

[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;}
}

我的查询有什么问题?

最佳答案

我不太明白你想要完成什么,但我可以解释你的问题。

A Where()子句是 Func<T, bool> .您传入 T 的实例它返回一个 bool值(value)。

在这一行.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条件排除任何 Settings没有 Files SettingFile在哪里是true .

_context.Settings
.Include(x => x.Files.Where(f => f.SettingFile == true))
.Where(x => x.Files.Any(f => f.SettingFile == true))
.ToList();

关于c# - Linq 包含和 Where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42116179/

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