Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 公共关联的linq查询_C#_Linq - Fatal编程技术网

C# 公共关联的linq查询

C# 公共关联的linq查询,c#,linq,C#,Linq,我正试图解决一个linq问题,我有一些用户可以加入多个组,现在我可以返回属于单个组的用户,如下所示: List<Student> students = new List<Student>(); public List<Student> ReturnStudentByGroupName(string groupName) { List<Student> student = (from g in students

我正试图解决一个linq问题,我有一些用户可以加入多个组,现在我可以返回属于单个组的用户,如下所示:

    List<Student> students = new List<Student>();
    public List<Student> ReturnStudentByGroupName(string groupName)
    {
        List<Student> student = (from g in students
                              where
                                  (from t in g.StudentGroup where t.GroupName == groupName select t).Count() > 0
                              select g).ToList();
        return student;
    }
List students=newlist();
公共列表ReturnStudentByGroupName(字符串groupName)
{
列表学生=(从学生中的g开始)
哪里
(从g.StudentGroup中的t开始,其中t.GroupName==GroupName选择t)。Count()>0
选择g.ToList();
留学生;
}
我现在的问题是我需要找到多个组的普通用户?例如,谁是A组和B组的普通成员。我不寻找这两个组的用户列表。如果用户属于这两个组,则只应返回用户

是否有人知道如何使用两个字符串作为输入,即stringfirstgroupname、stringsecondgroupname。那还普通学生吗

IEnumberstudentsofGroup(g组)
IEnumerable<Student> StudentsOfGroup(Group g)
{
    return students.Where(s => s.StudentGroup.Contains(g));
}

IEnumerable<Student> CommonStudents(IEnumerable<Group> groups)
{
    return groups
        .Select(StudentsOfGroup)
        .Aggregate((acc, g) => acc.Intersect(g));
}
{ 返回students.Where(s=>s.StudentGroup.Contains(g)); } IEnumerable CommonStudents(IEnumerable组) { 返回组 .选择(学生组) .骨料((acc,g)=>acc.Intersect(g)); }
或者,根据组的数量,以下速度可能更快:

IEnumberable<Student> CommonStudents(IEnumerable<Group> groups)
{
    var groupSet = new HashSet<Group>(groups);
    return students.Where(s => groupSet.IsSubsetOf(s.StudentGroup));
}
IEnumerable CommonStudent(IEnumerable组)
{
var groupSet=新哈希集(组);
返回students.Where(s=>groupSet.IsSubsetOf(s.StudentGroup));
}
IEnumerable组交叉点(IEnumerable组)
{
留学生
其中(s=>groups.All(g=>s.StudentGroup.Contains(g)));
}

您说过您只想返回属于a组和B组的用户列表,所以您自然只需要用两个条件而不是一个条件修改where语句

    List<Student> students = new List<Student>();
    public List<Student> GetIntersectingStudents(string groupOne, string groupTwo)
    {
        var student = from s in students
                      let grps = s.StudentGroup.ConvertAll(gn => gn.GroupName)
                      where grps.Contains(groupOne) && grps.Contains(groupTwo)
                      select s;
        return student.ToList();
    }
    public List<Student> GetIntersectingStudents(params string[] groups)
    {
        var student = from s in students
                      let grps = s.StudentGroup.ConvertAll(gn => gn.GroupName)
                      where !groups.Except(grps).Any()
                      select s;
        return student.ToList();
    }
List students=newlist();
公共列表GetIntersectingStudents(字符串groupOne、字符串GroupII)
{
var student=来自学生中的s
让grps=s.StudentGroup.ConvertAll(gn=>gn.GroupName)
其中grps.Contains(组一)和grps.Contains(组二)
选择s;
返回student.ToList();
}
公共列表GetIntersectingStudents(参数字符串[]组)
{
var student=来自学生中的s
让grps=s.StudentGroup.ConvertAll(gn=>gn.GroupName)
where!groups.Except(grps).Any()
选择s;
返回student.ToList();
}
这里有几个方法供您使用,其中一个方法在方法中包含两个参数(您要求的参数),另一个方法包含一个组列表(如果您需要从三个组中获取,而不是从两个组中获取,等等)

编辑:

我想我会把这个额外的方法也放进去,只是为了好玩。它汇编了所有组及其成员的列表

    public static Dictionary<string, List<Student>> GetStudentGroups()
    {
        var temp = from s in students
                   let grps = s.StudentGroup.ConvertAll(gn => gn.GroupName)
                   from grp in grps
                   group s by grp
                   into g
                   select g;
        return temp.ToDictionary(grping => grping.Key, studnt => studnt.ToList());
    }
公共静态字典GetStudentGroups()
{
var temp=来自学生中的s
让grps=s.StudentGroup.ConvertAll(gn=>gn.GroupName)
从grp到grp
按grp划分的s组
进入g
选择g;
返回temp.ToDictionary(grping=>grping.Key,studnt=>studnt.ToList());
}

那么如何从GET方法中查询它呢?如果您注意到我的“string groupName”,我可以在其中使用GET方法查询并返回结果。您可以轻松地重新编写代码以接受“string[]groups”作为输入。因为您有一个组类型,所以应该使用它。如果组只包含名称(这就是代码段中包含的全部内容),则可以轻松地将字符串列表转换为组对象列表。但在这种情况下,我想你也应该在组上实现Equals方法。或者只是重新编写代码来处理字符串…@JungleBoogie:是的,尤其是现在我刚刚修复了它。哈哈,是的,我也注意到了字符串,我们只是想尝试一下,并提前接受我的道歉。啊,你刚刚更改了你的代码,哈哈,这是一个还是之前的一个?是的,经过测试,工作方式正是我需要的!谢谢Caesay
    public static Dictionary<string, List<Student>> GetStudentGroups()
    {
        var temp = from s in students
                   let grps = s.StudentGroup.ConvertAll(gn => gn.GroupName)
                   from grp in grps
                   group s by grp
                   into g
                   select g;
        return temp.ToDictionary(grping => grping.Key, studnt => studnt.ToList());
    }