如何使用嵌套属性中的select获取数据-linq

问题描述 投票:0回答:1

这是我的数据结构:

public class Product
{
    // Rest of props

    public ICollection<ProductUpdate> ProductUpdate { get; set; }
}

public class ProductUpdate 
{
    // Rest of props

    public Delivery Delivery { get; set; }
}

public class Delivery
{
    // Rest of props

    public virtual ICollection<DeliveryUsersApprovers> DeliveryUsersApprovers { get; set; }
}

public class DeliveryUsersApprovers
{
    // Rest of props

    public User User { get; set; }
}

如何编写linq方法语法查询,该查询将从Id中选择StartDateNoteProductUpdate,同时为每行选择ProductUpdate对应用户,该用户在[ C0]类我想使用DeliveryUsersApprovers仅获得所需的列来实现它。

我已经尝试过类似的方法,但是那是行不通的:

.Select()

我真的很想深入嵌套道具并为每个var paymentStatusUpdates = await _dbContext.Product.Include(x => x.ProductUpdate) .Select(x => new SomeCustomClassObjectWithFourProperties { // Read data from ProductUpdate somehow and select Id, Date and Note from ProductUpdate and get User from nested property .Select(y=> new SomeCustomClassObjectWithFourProperties { Id = y.Id, Date=y.StartDate, Note=y.Note, User=? // this is User from very nested prop }) }) .FirstOrDefaultAsync(x => x.Id == productId, cancellationToken); //productId is received in method params 达到User,所以任何形式的帮助都将非常有用!

谢谢

这是我的数据结构:public class Product {//其余道具public ICollection ProductUpdate {get;组; }} public class ProductUpdate {//其他道具...

c# linq linq-to-sql linq-to-entities entity
1个回答
0
投票

使用ProductUpdate展平SelectMany集合,然后将每个DeliveryUsersApprovers映射到包含DeliveryUsersApproversResult数据的User对象。

© www.soinside.com 2019 - 2024. All rights reserved.