LINQ。加载相关元素

时间:2019-07-09 07:08:21

标签: c# entity-framework linq many-to-many

我有一个实体ContractsListKindWorksKindWorks

public partial class Contracts
{
    public Contracts()
    {
        ListKindWorks = new HashSet<ListKindWorks>();
    }

    public int Id { get; set; }
    ...
    public virtual ICollection<ListKindWorks> ListKindWorks { get; set; }
}

public partial class ListKindWorks
{
    public int IdContract { get; set; }
    public int IdKindWork { get; set; }

    public virtual Contracts IdContractNavigation { get; set; }
    public virtual KindWorks IdKindWorkNavigation { get; set; }
}

public partial class KindWorks
{
    public KindWorks()
    {
        ListKindWorks = new HashSet<ListKindWorks>();
    }

    public int Id { get; set; }
    public string Title { get; set; }
    public virtual ICollection<ListKindWorks> ListKindWorks { get; set; }
}

我想加载相关元素。像这样的伪代码:

source = model.Contracts
    .Select(c => new MyType
    {
        IdContract = c.Id,
        KindWork = new List<Item>
        { Id = KindWorks.Id, Value = KindWorks.Title }

        // or
        //    KindWork = c.ListKindWorks
        //           .Where(x => x.IdContract == c.Id)
        //           .Select(y => new Item 
        //           { Id = y.IdKindWork, Value = y.IdKindWorkNavigation.Title })
        ... 
    })
    .ToList();

public class Item
{
    public int Id { get; set; }
    public string Value { get; set; }
}

我可以为每个List<Item>加载Contracts吗?

2 个答案:

答案 0 :(得分:0)

如果我了解您要寻找的内容,则会为词典中的每个合同创建一个清单。这是我的结果:

var contracts = new List<Contracts>
{
    new Contracts { Id = 1 },
    new Contracts { Id = 2 },
    new Contracts { Id = 3 },
};

var listKindWorks = new List<ListKindWorks>
{
    new ListKindWorks { IdContract = 1, IdKindWork = 1 },
    new ListKindWorks { IdContract = 1, IdKindWork = 2 },
    new ListKindWorks { IdContract = 2, IdKindWork = 2 },
    new ListKindWorks { IdContract = 2, IdKindWork = 3 }
};

var kindWorks = new List<KindWorks>
{
    new KindWorks { Id = 1, Title = "Title 1" },
    new KindWorks { Id = 2, Title = "Title 2" },
    new KindWorks { Id = 3, Title = "Title 3" },
};

Dictionary<Contracts, List<Item>> myDic = contracts.Select(
    contract => contract).ToDictionary(
    contract => contract,
    contract => listKindWorks.Where(
            listKindWork => listKindWork.IdContract.Equals(contract.Id))
        .Select(listKindWork => new Item
        {
            Id = kindWorks.FirstOrDefault(kindWork => kindWork.Id.Equals(listKindWork.IdKindWork))?.Id?? listKindWork.IdKindWork,
            Value = kindWorks.FirstOrDefault(kindWork => kindWork.Id.Equals(listKindWork.IdKindWork))?.Title?? "KindWork not found"
        }).ToList());

我通过测试得到了这个

Contract1 : Title1, Title2
Contract2 : Title2, Title3
Contract3 : Nothing

答案 1 :(得分:0)

    IEnumerable<Item> KindWork = c.ListKindWorks
    .Select(y => new Item 
    { 
        Id = y.IdKindWork, 
        Value = y.IdKindWorkNavigation.Title
    })

    IEnumerable<Item> Subject = c.ListSubjects
    .Select(y => new Item
    { 
        Id = y.IdSubject, 
        Value = y.IdSubjectNavigation.Title
    })