Как применить предложениеwhere к результату ICollection LINQ? ⇐ C#
Как применить предложениеwhere к результату ICollection LINQ?
I have three class in my project:
public partial class Product { public string? Title { get; set; } public string? UrlAddress { get; set; } public virtual ICollection ProductSizes { get; set; } public virtual Picture? Picture { get; set; } } public partial class ProductSize { public int Id { get; set; } public int ProductId { get; set; } public int SizeId { get; set; } public virtual Product Product { get; set; } = null!; public virtual Size Size { get; set; } = null!; } public partial class Size { public int Id { get; set; } public int? ProductTypeId { get; set; } public string Name { get; set; } = null!; } And try to get something result with this linq query:
var result = _tebpooshContext.Products .Where(p => p.ProductCategoryId == 1 && p.ProductTypeId == 2 ) .Include(p => p.Picture) .Include(p => p.ProductSizes) .ThenInclude(p => p.Size) .Select(p => new TileProductDto { Image = p.Picture.Url.Replace("SYSTEM_TYPE_SYNCSERVER", "URL"), Name = p.Name.Trim(), Price = p.Price.ToString(), ProductSizes = p.ProductSizes, ProdutId = p.Id.ToString(), }) .Take(9).ToList(); Now try to write this where clause before Take(9) with this syntax:
.Where(p => p.ProductSizes.Where(p => p.Size.Name == "42")) but get this error :
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'bool' Try to fix that error with this way :
.Where(p => p.ProductSizes.Any(p => p.Size.Name == "42"))
But that where clause return all product size and size "42" dont effect on where clause.
How can i apply my where clause before Take(9) ?
Источник: https://stackoverflow.com/questions/780 ... inq-result
I have three class in my project:
public partial class Product { public string? Title { get; set; } public string? UrlAddress { get; set; } public virtual ICollection ProductSizes { get; set; } public virtual Picture? Picture { get; set; } } public partial class ProductSize { public int Id { get; set; } public int ProductId { get; set; } public int SizeId { get; set; } public virtual Product Product { get; set; } = null!; public virtual Size Size { get; set; } = null!; } public partial class Size { public int Id { get; set; } public int? ProductTypeId { get; set; } public string Name { get; set; } = null!; } And try to get something result with this linq query:
var result = _tebpooshContext.Products .Where(p => p.ProductCategoryId == 1 && p.ProductTypeId == 2 ) .Include(p => p.Picture) .Include(p => p.ProductSizes) .ThenInclude(p => p.Size) .Select(p => new TileProductDto { Image = p.Picture.Url.Replace("SYSTEM_TYPE_SYNCSERVER", "URL"), Name = p.Name.Trim(), Price = p.Price.ToString(), ProductSizes = p.ProductSizes, ProdutId = p.Id.ToString(), }) .Take(9).ToList(); Now try to write this where clause before Take(9) with this syntax:
.Where(p => p.ProductSizes.Where(p => p.Size.Name == "42")) but get this error :
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'bool' Try to fix that error with this way :
.Where(p => p.ProductSizes.Any(p => p.Size.Name == "42"))
But that where clause return all product size and size "42" dont effect on where clause.
How can i apply my where clause before Take(9) ?
Источник: https://stackoverflow.com/questions/780 ... inq-result
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение