简体   繁体   English

实体框架中的复杂图形

[英]Complex graphing in entity framework

I can't seem to find the correct where clause to get only the items I need. 我似乎找不到正确的where子句来仅获取我需要的项目。

I have Divisions, these contain Categories en these contain Items. 我有部门,这些部门包含类别,这些部门包含项目。 These are the classes: 这些是类:

public class Division    {
        public string Description { get; set; }

        public List<Category> Categories { get; set; }
    }

public class Category : IdEntity
{
    public string Description { get; set; }

    public Guid DivisionId { get; set; }

    public List<Item> Items { get; set; }
}

public class Item
{
    public Guid CategoryId { get; set; } 

    public DateTime Date { get; set; }

    public string Description { get; set; }
}

What I need is a division with the Id in a parameter, all the categories from this division and the items that have a certain date for each category. 我需要的是在参数中包含ID的划分,该划分的所有类别以及每个类别具有特定日期的项目。

So right now I do this: 所以现在我这样做:

public Division GetFullDivisionByIdAndDate(Guid id, DateTime date)
    {
        using (new ChangeTrackingScope(ChangeTracking.Disabled))
        {
            var divisionGraph = new Graph<Division>().Include(d => d.Categories.Select(c => c.Items));
            var division = _divisionDL.GetFullDivisionByIdAndDate(id, divisionGraph, date);
            return division;
        }
    }

And than in the DL I do 比起DL我

        public Division GetFullDivisionByIdAndDate(Guid id, Graph<Division> graph, DateTime date)
    {
        using (var db = new ContextScope<DatabaseContext>())
        {
            var q = graph.ApplySetReferences(db.Context.Divisions).AsNoTracking();
            return q.SingleOrDefault(p => p.Id == id);
        }
    }

Here I get the division with all its categories (so far so good) but I also get all items and I need only the items with the date given as parameter. 在这里,我得到了所有类别的划分(到目前为止很好),但是我也得到了所有项目,并且我只需要带有日期作为参数的项目。 Anyone has an idea how to do this? 有人知道如何执行此操作吗?

Your code is not very accessible because of a few missing methods ( Graph , ApplySetReferences ) so I can't hook into it. 由于缺少一些方法( GraphApplySetReferences ),因此您的代码不是很容易访问,因此我无法了解它。 But I can show a common way to query an object graph, which is by navigation properties. 但是我可以展示一种查询对象图的通用方法,即通过导航属性。 In you model, a basic query body could look like this: 在您的模型中,基本查询主体可能如下所示:

from d in Divisions
from c in d.Categories
from i in c.Items
select new { Div = d.Description, Cat = c.Description, Item = i.Description }

Starting from here you can add other filters and properties, like 从这里开始,您可以添加其他过滤器和属性,例如

from d in Divisions.Where(div => div.Id == id)
from c in d.Categories
from i in c.Items.Where(item => item.Date == date)
select new { ... }

Or if you want a result with items in a filtered collection: 或者,如果您想要一个包含过滤集合中项目的结果:

from d in Divisions.Where(div => div.Id == id)
from c in d.Categories
select new new { Div = d.Description,
                 Cat = c.Description,
                 Items = c.Items.Where(item => item.Date == date)
               }

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

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