简体   繁体   English

加载相关实体

[英]loading related entities

We are using EF .NET core for our architecture and want to do a basic query. 我们正在将EF .NET核心用于我们的体系结构,并希望进行基本查询。 So all we are after is using both LINQ & EF with lazy loading switched off to select the parent, in this case stick item and some of the fields in the child objects. 因此,我们所要做的就是同时使用LINQ和EF,同时关闭延迟加载以选择父对象,在这种情况下,是棍子项目和子对象中的某些字段。 Then return them back into our strongly typed item. 然后将它们返回到我们的强类型项中。

Something like this. 这样的事情。

 var qry = _context.Set<stock>()
        .Include(p => p.stockitem)
        .Where(q => q.customer == accountNo)
        .Select(r => new stock() { 
            customer = r.customer, 
            r.stockitem.select(s => new {id, s.id, s.name }})
        .ToList();

So is it possible to do this? 那么有可能这样做吗? basically get hold of say just a couple of columns from our child object. 基本上可以说我们的子对象只有几列。 Then have everything returned in the strongly typed object. 然后,将所有内容都返回到强类型对象中。

First create a model in which the selected data will be stored (This model is just an example): 首先创建一个模型,其中将存储所选数据(该模型只是一个示例):

public class MyNewCustomer
{
    public string CustomerAccount { get; set; }
    public Dictionary<int, string> Items { get; set; }
}

After that you can create a new object from your select: 之后,您可以从选择中创建一个新对象:

var data = _context.Stocks
    .Include(p => p.stockitem)
    .Where(p => p.customer == accountNo)
    .Select(p => new MyNewCustomer 
    {
        CustomerAccount = p.customer,
        Items = p.stockitem.ToDictionary(s => s.id, s => s.name)
    }).ToList();

PS: I used Dictionary because you didn't provide the actual model. PS:之所以使用Dictionary是因为您没有提供实际的模型。 You can choose whatever kind of List<TObject> you want. 您可以选择任何所需的List<TObject>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM