I have the following linq statement:
var result = _context.Settings
.Include(x => x.Files)
.Where(y => y.Files.Where(f => f.SettingFile == true))
.Select(z => z.Deleted == null)
.ToList();
The thing I want to do is to get all the settings from the table. I also want to include the files-table and get all the files that has the SettingFile true. But I keep get the following error:
Cannot implicity convert type IEnumerable(UploadedFile) to bool.
Here Is my UploadedFile-model:
[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; }
Here is my Setting-model:
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; }
}
Modelbase:
public abstract class ModelBase : EntityBase
{
public DateTime? Deleted {get; set;}
}
What am I doing wrong with my query?
I don't quite understand what you're trying to accomplish, but I can explain your issue.
A Where()
clause is a Func<T, bool>
. You pass in an instance of T
and it returns a bool
value.
In this line .Where(y => y.Files.Where(f => f.SettingFile == true))
, you're returning a IEnumerable<T>
where it should be returning a bool
. This is what's causing the error. You can resolve the error by changing the y.Files.Where(f => f.SettingFile == true)
to y.Files.Any(f => f.SettingFile)
or y.Files.All(f => f.SettingFile)
. However, I don't think this is what you're trying to accomplish.
Edit: Since you're trying to get all the Settings that have the SettingFile == true
then do the following:
_context.Settings
.Include(x => x.Files.Where(f => f.SettingFile == true))
.Select(z => z.Deleted == null)
.ToList();
Edit: After further communication in the comments, you said you wanted a collection of Setting
. So you'll just need to remove the .Select(x => z.Deleted == null)
and you should be golden.
_context.Settings
.Include(x => x.Files.Where(f => f.SettingFile == true))
.ToList();
I don't know your use case, but it might be advisable to add another Where
condition to exclude any Settings
that don't have Files
where the SettingFile
is true
.
_context.Settings
.Include(x => x.Files.Where(f => f.SettingFile == true))
.Where(x => x.Files.Any(f => f.SettingFile == true))
.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.