具有相同类型但有不同限制的多个列表属性的类 [英] Class with multiple List Properties of same type but with different restrictions

查看:50
本文介绍了具有相同类型但有不同限制的多个列表属性的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:我有一个类,该类具有2个相同类类型的列表属性(但是在填充方式上有一些不同的限制),比如:

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:

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?

为进一步说明,应根据以下条件填充团队类的2个列表属性:

To clarify a bit more, the 2 list properties of the team class should be filled according to:

1-成员属性-应包括没有经验的所有相关编程器(proggramer.Experience == null或);

1 - Members attribute - Should include all related proggramers with no experience (proggramer.Experience == null or "");

2-领导者属性-应包括具有任何经验的所有相关编程器(programmer.Experiente!= null或);

2 - Leaders attribute - Should include all related proggramers with any experience (programmer.Experiente != null nor "");

这是MyProjectDbContext声明:

EDIT 2: Here's the MyProjectDbContext declaration:

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

推荐答案

您在谈论EntityFramework(对实体的Linq),对吗?如果是这样,则Include()是Linq To Entities的一种方法,可在结果集中包含子关系.我认为您应该将Where()放在Inlcude()之外.

You are talking about EntityFramework (Linq to entities) right? If so, Include() is a Method of Linq To Entities to include a sub-relation in the result set. I think you should place the Where() outside of the Inlcude().

本主题中,您会找到一些示例有关如何使用Include()方法的信息.

On this topic you'll find some examples on how to use the Include() method.

因此,我建议首先添加Include()以包括成员"和领导者"关系,然后应用您的Where-Statement(可以通过一个Where()完成).

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()).

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

对于我来说不清楚的是,您在谈论获取与领导者和成员组成的团队列表时的标准和用例.上面的示例可能会返回与Where()语句匹配的团队列表.您可以查看它,并在该循环中可以列出其成员和领导者(如果使用的话).

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. You can look though it and within that loop you can list its members and leaders - if that is the use-case.

另一种选择是这样的:

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

这将为您提供没有经验的成员列表(按团队分组).您可以在其成员上循环组(团队).如果您只想让每个团队一次,则可以在末尾添加一个Distinct(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.

希望这会有所帮助.如果您需要一些更详细的代码示例,则有助于更好地了解您的要求.因此,也许您可​​以对查询的期望再说几句话.

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.

更新:

只需阅读我们听起来很有趣的编辑.我认为您无法在一个Linq-To-Entities语句中完成全部操作.就个人而言,我会在成员和领导者属性的获取器上执行此操作,这些属性将执行自己的查询(作为只读属性).为了获得海量数据的性能,我什至可以使用数据库本身的SQL视图来实现.但这在一定程度上取决于所使用的成员"和领导者"的上下文(频繁出现等).

Just read our edits which sound interesting. I don't think you can do this all in one Linq-To-Entities statement. 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. But this depends a little on the context the "Members" and "Leaders" are used (high frequent etc).

更新2:

使用单个查询获取具有成员和领导者子列表的团队表,我将对程序员"进行查询,并按团队"和经验"对它们进行分组.然后,结果是其中具有程序员的组(有经验/无经验)的组(=团队)列表.然后可以使用三个嵌套的foreach-Statement构建最终表.有关某些分组示例,请参见此处(请参见示例"GroupBy -嵌套").

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. See here for some grouping examples (see the example "GroupBy - Nested").

这篇关于具有相同类型但有不同限制的多个列表属性的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆