Использование LINQ Projection для извлечения глубоко вложенных свойствC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Использование LINQ Projection для извлечения глубоко вложенных свойств

Сообщение Anonymous »

Журналы, которые я получаю:

Код: Выделить всё

Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync

Описание проблемы:
Я пытаюсь написать запрос для получения некоторых сведений о заказе вместе с их продуктами (которые имеют цвет, размер и т. д.). ). Я использую проекцию, но мне нужно получить доступ к некоторым глубоко вложенным свойствам, чтобы получить цену за измерение.
ОБНОВЛЕНИЕ: здесь я получаю все данные из таблицы соединения

Код: Выделить всё

 var allProductsWithDimensions = await productsOnDimensionsRepository.GetAllAsync();
Это запрос, который я написал:

Код: Выделить всё

var allProductsOnCurrentOrder = await productsOnOrdersRepository
.FindQueryable(pc => pc.IdComanda == order.IdComandaDto)
.Select(pc => new ProductsOnOrdersDto
{
IdProdusDto = pc.Produs.IdProdus,
CodProdusDto = pc.Produs.CodProdus,
NumeProdusDto = pc.Produs.NumeProdus!,
PretBazaProdusDto = pc.Produs.PretDeBaza,
NumeProducatorDto = pc.Produs.Producator == null ? null : pc.Produs.Producator.NumeProducator,
TipulProdusuluiDto = pc.Produs.TipulProdusului,
NumeSetDto = pc.Set == null ? null : pc.Set.NumeSet,
PretSetDto = pc.Set == null ? null : pc.Set.PretSet,
PretRedusSetDto = pc.Set == null ? null : pc.Set.PretRedusSet,
NumeCuloareDto = pc.PcCuloare.NumeCuloare,
CodCuloareDto = pc.PcCuloare.CodCuloare.CodCuloare!,
LungimeDto = pc.PcDimensiune == null ? null : pc.PcDimensiune.Lungime,
LatimeDto = pc.PcDimensiune == null ? null : pc.PcDimensiune.Latime,
RecomandarePatDto = pc.PcDimensiune == null ? null : pc.PcDimensiune.RecomandarePat,
PretDto = pc.PcDimensiune == null
? null
: (from pcd in allProductsWithDimensions
where pcd.IdProdus == pc.Produs.IdProdus && pcd.IdDimensiune == pc.PcDimensiune.IdDimensiune
select pcd.Pret).First(),
PretRedusDto = pc.PcDimensiune == null
? null
: (from pcd in allProductsWithDimensions
where pcd.IdProdus == pc.Produs.IdProdus && pcd.IdDimensiune == pc.PcDimensiune.IdDimensiune
select pcd.PretRedus).First(),
PerdeaEstePerecheDto = pc.PcDimensiune == null ? null : pc.PcDimensiune.PerdeaEstePereche,
TipGalerieCusaturaDto =
pc.PcManopera == null ? null : pc.PcManopera.TipGalerieLaManopera.NumeTipGalerie,
PretTipGalerieCusaturaDto =
pc.PcManopera == null ? null : pc.PcManopera.TipGalerieLaManopera.PretTipGalerie,
TipLinieCusaturaDto = pc.PcManopera == null ? null : pc.PcManopera.TipLinieLaManopera.NumeTipLinie,
PretTipLinieCusaturaDto =
pc.PcManopera == null ? null : pc.PcManopera.TipLinieLaManopera.PretPeTipLinie,
InelePrindereDto = pc.PcManopera == null ? null : pc.PcManopera.InelPrindereLaManopera!.CuloareInel,
PretInelPrindereDto = pc.PcManopera == null
? null
: pc.PcManopera.InelPrindereLaManopera!.PretPerMetruInele,
MaterialDto = pc.PcManopera == null ? null : pc.PcManopera.MaterialLaManopere.NumeMaterial,
PretMaterialDto = pc.PcManopera == null ? null : pc.PcManopera.MaterialLaManopere.PretMaterial,
})
.ToListAsync();
Проблема возникает здесь:

Код: Выделить всё

 PretDto = pc.PcDimensiune == null
? null
: (from pcd in allProductsWithDimensions
where pcd.IdProdus == pc.Produs.IdProdus && pcd.IdDimensiune == pc.PcDimensiune.IdDimensiune
select pcd.Pret).First(),
PretRedusDto = pc.PcDimensiune == null
? null
: (from pcd in allProductsWithDimensions
where pcd.IdProdus == pc.Produs.IdProdus && pcd.IdDimensiune == pc.PcDimensiune.IdDimensiune
select pcd.PretRedus).First(),
Это ProduseCuComenzi со всеми подробностями:

Код: Выделить всё

public class ProduseCuComenzi
{
// Attributes
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int IdProduseCuComenzi { get; set;  }
public int NrBucati { get; set; }
// Fk
public int? IdSet { get; set; }
public Seturi? Set { get; set; }
public int IdProdus { get; set; }
public Produse Produs { get; set; } = null!;
public int IdComanda { get; set; }
public Comenzi Comanda { get; set; } = null!;
public int IdCuloare { get; set; }
public Culori PcCuloare { get; set; } = null!;
public int? IdDimensiune { get; set; }
public Dimensiuni? PcDimensiune { get; set; }
public int? IdManopera { get; set; }
public Manopere? PcManopera { get; set; }

}
Это класс Dimensions:

Код: Выделить всё

public class Dimensiuni
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int IdDimensiune { get; init; }

[StringLength(4)]
public string Lungime { get; init; } = null!;

[StringLength(4)]
public string Latime { get; init; } = null!;

public bool? PerdeaEstePereche { get; set; }

[StringLength(15)]
public string? RecomandarePat { get; set; }
// navigation props
public ICollection? DProduseCuDimensiuni { get; }
public ICollection? DAsociereSeturi { get; }
public ICollection? DProduseCuComenzi { get; }
А это ОБЪЕДИНЯЕМАЯ ТАБЛИЦА ProduseCuDimensiuni, где у меня есть цена и скидка:

Код: Выделить всё

public class ProduseCuDimensiuni
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int IdProdusCuDimensiune { get; init; }

// Foreign Keys
public decimal Pret { get; set; }
public decimal PretRedus { get; set; }
public int? IdDimensiune { get; set; }
public Dimensiuni? PdDimensiune { get; set; }
public int IdProdus { get; set; }
public  Produse  PdProduse { get; set; } = null!;}
Что здесь нужно сделать, чтобы получить Pret и PretRedus и избежать ошибки? Нужно ли использовать JOIN между ProduseCuDimensiuni и ProduseCuComenzi?


Подробнее здесь: https://stackoverflow.com/questions/790 ... properties
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»