简体   繁体   English

具有多个相同类型但具有不同限制的列表属性的类

[英]Class with multiple List Properties of same type but with different restrictions

Here's my problem: I have a class that have 2 list properties of the same class type (but with some different restriction as on how to be filled), let's say: 这是我的问题:我有一个类,该类具有2个相同类类型的列表属性(但是在填充方式上有一些不同的限制),让我们说:

public class Team 
{
    [Key]
    public int IDTeam { get; set; }

    public string TeamName { get; set; }

    public List<Programmer> Members { get; set; }

    public List<Programmer> Leaders { get; set; }

    public LoadLists(MyProjectDBContext db) 
    {
        this.Members = db.Programmers.Where(p => p.IDTeam = this.IDTeam 
                 && (p.Experience == "" || p.Experience == null)).ToList();

        this.Leaders = db.Programmers.Where(p => p.IDTeam = this.IDTeam 
                 && (p.Experience != null && p.Experience != "")).ToList();
    }
}

public class Programmer 
{
    [Key]
    public int IDProgrammer { get; set; }

    [ForeignKey("Team")]
    public int IDTeam { get; set; }
    public virtual Team Team { get; set; }

    public string Name { get; set; }

    public string Experience { get; set; }
}

At some point, I need to take a list of Teams, with it's members and leaders, and for this I would assume something like: 在某个时候,我需要列出一个团队列表,包括其成员和领导者,为此,我假设类似:

return db.Teams
    .Include(m => m.Members.Where(p => p.Experience == "" || p.Experience == null)
    .Include(l => l.Leaders.Where(p => p.Experience != null && p.Experience != "")
    .OrderBy(t => t.TeamName)
    .ToList();

And, of course, in this case I would be assuming it wrong (cause it's not working at all). 而且,当然,在这种情况下,我会假设它是错误的(因为它根本无法工作)。 Any ideas on how to achieve that? 关于如何实现这一目标的任何想法?

EDIT: To clarify a bit more, the 2 list properties of the team class should be filled according to: 编辑:为了澄清一点,团队类的2个列表属性应根据以下条件填充:

1 - Members attribute - Should include all related proggramers with no experience (proggramer.Experience == null or ""); 1-成员属性-应包括没有经验的所有相关编程器(proggramer.Experience == null或““);

2 - Leaders attribute - Should include all related proggramers with any experience (programmer.Experiente != null nor ""); 2-领导者属性-应包括具有任何经验的所有相关编程器(programmer.Experiente!= null或““);

EDIT 2: Here's the MyProjectDbContext declaration: 编辑2:这是MyProjectDbContext声明:

public class MyProjectDBContext : DbContext
{
    public DbSet<Team> Teams { get; set; }
    public DbSet<Programmer> Programmers { get; set; }
}

You are talking about EntityFramework (Linq to entities) right? 您是在谈论EntityFramework(实体的Linq),对吗? If so, Include() is a Method of Linq To Entities to include a sub-relation in the result set. 如果是这样,则Include()是Linq To Entities的一种方法,可在结果集中包含子关系。 I think you should place the Where() outside of the Inlcude(). 我认为您应该将Where()放在Inlcude()之外。

On this topic you'll find some examples on how to use the Include() method. 该主题上,您将找到一些有关如何使用Include()方法的示例。

So I suggest to add the Include()'s first to include the relations "Members" and "Leaders" and then apply your Where-Statement (can be done with one Where()). 因此,我建议首先添加Include()以包括“成员”和“领导者”关系,然后应用您的Where-Statement(可以通过一个Where()完成)。

return db.Teams
    .Include("Team.Members")
    .Include("Team.Leaders")
    .Where(t => string.IsNullOrWhitespace(t.Members.Experience) ... )

What is unclear to me is your where criteria and your use-case at all as you are talking of getting a list of Teams with Leaders and Members. 对我来说不清楚的是,您在谈论获取包含领导者和成员的团队列表时的标准和用例。 May above example will return a list of Teams that match the Where() statement. 上面的示例可能会返回与Where()语句匹配的团队列表。 You can look though it and within that loop you can list its members and leaders - if that is the use-case. 您可以查看它,并在该循环中可以列出其成员和领导者(如果使用的话)。

An alternative is something like this: 替代方法是这样的:

return db.Members
    .Where(m => string.IsNullOrWhitespace(m.Experience))
    .GroupBy(m => m.Team)

This get you a list of members with no experience grouped by Team. 这会为您提供没有经验的成员列表,这些成员按团队分组。 You can loop the groups (Teams) and within on its members. 您可以在其成员上循环组(团队)。 If you like to get each team only once you can add a Distinct(m => m.Team) at the end. 如果您只想让每个团队一次,则可以在末尾添加一个Distinct(m => m.Team)。

Hope this helps. 希望这可以帮助。 If you need some more detailed code samples it would help to understand your requirements better. 如果您需要一些更详细的代码示例,则有助于更好地了解您的要求。 So maybe you can say a few more words on what you expect from the query. 因此,也许您可​​以对查询的期望再说几句话。

Update: 更新:

Just read our edits which sound interesting. 只需阅读我们听起来很有趣的编辑即可。 I don't think you can do this all in one Linq-To-Entities statement. 我认为您无法在一个Linq-To-Entities语句中完成全部操作。 Personally I would do that on the getters of the properties Members and Leaders which do their own query (as a read-only property). 就个人而言,我会在成员和领导者属性的获取器上执行此操作,这些属性将执行自己的查询(作为只读属性)。 To get performance for huge data amount I would even do it with SQL-views on the DB itself. 为了获得海量数据的性能,我什至可以使用数据库本身的SQL视图来实现。 But this depends a little on the context the "Members" and "Leaders" are used (high frequent etc). 但这在某种程度上取决于所使用的“成员”和“领导者”的上下文(频繁出现等)。

Update 2: 更新2:

Using a single query to get a table of teams with sublists for members and leaders I would do a query on "Programmers" and group them nested by Team and Experience. 使用单个查询获取具有成员和领导者子列表的团队表,我将对“程序员”进行查询,并按“团队”和“经验”对它们进行分组。 The result is then a list of groups (=Teams) with Groups (Experienced/Non-experience) with Programmers in it. 然后,结果是其中包含程序员的组(有经验/无经验)的组(=团队)列表。 The final table then can be build with three nested foreach-Statements. 然后可以使用三个嵌套的foreach-Statement构建最终表。 See here for some grouping examples (see the example "GroupBy - Nested"). 有关某些分组示例,请参见此处 (请参见示例“ GroupBy-嵌套”)。

Whenever you fetch entities, they will be stored in the context -- regardless of the form they are "selected" in. That means you can fetch the teams along with all the necessary related entities into an anonymous type, like this: 每当您获取实体时,它们都会存储在上下文中-不管“选择”它们的形式如何。这意味着您可以将团队以及所有必要的相关实体获取为匿名类型,如下所示:

var teams =
    (from team in db.Teams
     select new {
         team,
         relatedProgrammers = team.Programmers.Where(
                                   [query that gets all leaders OR members])
     }).ToList().Select(x => x.team);

It looks like we're throwing away the relatedProgrammers field here, but those Programmer entities are still in memory. 看起来我们在这里扔掉了相关的relatedProgrammers字段,但是那些Programmer实体仍在内存中。 So, when you execute this: 因此,执行此命令时:

foreach (var team in teams) team.LoadLists(db);

...it will populate the lists from the programmers that were already fetched, without querying the database again (assuming db is the same context instance as above). ...它将填充已经获取的程序员的列表,而无需再次查询数据库(假设db与上面的上下文实例相同)。

Note: I haven't tested this myself. 注意:我自己尚未测试过。 It's based on a similar technique shown in this answer . 它基于此答案中显示的类似技术。

EDIT - Actually, it looks like your "leaders" and "members" cover all programmers associated with a team, so you should be able to just do Teams.Include(t => t.Programmers) and then LoadLists . 编辑 -实际上,您的“领导者”和“成员”似乎覆盖与团队相关的所有程序员,因此您应该能够只使用Teams.Include(t => t.Programmers) ,然后执行LoadLists

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

相关问题 属性相同,但类名不同-XML序列化 - Same properties but different class name - XML Serialization 在一个类中具有两个相同类型的导航属性 - Having two navigation properties of the same type in a class 多次实现包含具有不同类型参数属性的同一个通用接口 - Implement multiple times the same generic interface that includes properties with different type parameters 在不同列表的属性中获取具有相同值的项目 - Get items with same values in properties in different list 如何为一个类创建具有相同属性但值与每个对象不同的多个对象 - how to create multiple objects with same properties for a class but values are different to each object 具有相同属性但类型不同的多个类 - Multiple classes with same properties but different types 在每个类中使用不同的格式序列化同一类中的多个DateTime属性 - Serializing multiple DateTime properties in the same class using different formats for each one EF引用同一类型的多个属性 - EF multiple properties referencing same type 相同类型的多个导航属性的关系配置? - Relashionship configuration of multiple navigation properties of same type? 向多个类型参数添加类型限制? - Add type restrictions to multiple type parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM