仅当类别类型计数大于1时,Linq groupby IdCordinator
需要按IdCordinator对记录进行分组, 条件仅当类别类型计数大于1时,Linq groupby IdCordinator,linq,count,group-by,where,Linq,Count,Group By,Where,需要按IdCordinator对记录进行分组, 条件 如果类别类型不等于“S”,则包括在组中 如果特定协调员的类别类型为“S”,且计数大于1,则将其包括在组中 如果特定协调员的类别类型为“S”,且计数等于1,则不包括在组中 我应该如何构建where子句。如果我有一个where子句,如下所示,如果只有一个元素is list,尽管它不属于类别“S”,它返回0条记录 using System; using System.Collections.Generic; using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqGroupbyWhere
{
class Program
{
static void Main( string[] args )
{
StudentList studentList = new StudentList();
studentList.getList();
Console.ReadKey();
}
}
public class StudentList
{
public void getList()
{
List<Students> stdLst = new List<Students>();
stdLst.Add( new Students( "stud1", 20, "M", "I001" ) );
stdLst.Add( new Students( "stud2", 20, "M", "I001" ) );
stdLst.Add( new Students( "stud3", 20, "M", "I002" ) );
stdLst.Add( new Students( "stud4", 20, "M", "I003" ) );
stdLst.Add( new Students( "stud5", 20, "S", "I001" ) );
stdLst.Add( new Students( "stud6", 20, "S", "I001" ) );
stdLst.Add( new Students( "stud7", 20, "S", "I002" ) );
var lst = stdLst.GroupBy( s => s.IdCordinator )
.SelectMany( sg =>
sg.Where( s => !s.Category.Equals( "S" ) || sg.Count( c => c.Category.Equals( "S" ) ) > 1 )
.Select( student => student.Name ) )
.OrderBy( o => o );
Console.WriteLine( string.Join( ",", (string[]) lst.ToArray() ) );
}
}
public class Students
{
public Students( string name, int age, string cat, string siCordinator )
{
this.Name = name;
this.Age = age;
this.Category = cat;
this.IdCordinator = siCordinator;
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public string Category
{
get;
set;
}
public string IdCordinator
{
get;
set;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间LinqGroupbyWhere
{
班级计划
{
静态void Main(字符串[]参数)
{
StudentList StudentList=新建StudentList();
getList();
Console.ReadKey();
}
}
公共班级学生名单
{
public void getList()
{
List stdLst=新列表();
标准添加(新生(“stud1”,20,“M”,“I001”);
标准添加(新生(“stud2”,20,“M”,“I001”);
标准添加(新生(“stud3”,20,“M”,“I002”);
标准添加(新生(“stud4”,20,“M”,“I003”);
标准添加(新生(“stud5”,20,“S”,“I001”);
标准添加(新生(“stud6”,20,“S”,“I001”);
标准添加(新生(“stud7”,20,“S”,“I002”);
var lst=stdLst.GroupBy(s=>s.IdCordinator)
.SelectMany(sg=>
其中(s=>!s.Category.Equals(“s”)| sg.Count(c=>c.Category.Equals(“s”)>1)
.Select(student=>student.Name))
.OrderBy(o=>o);
Console.WriteLine(string.Join(“,”,(string[])lst.ToArray());
}
}
公立班学生
{
公立学生(字符串名称、整数、字符串类别、字符串序号)
{
this.Name=Name;
这个。年龄=年龄;
这个类别=猫;
this.IdCordinator=sicodinator;
}
公共字符串名
{
得到;
设置
}
公共信息
{
得到;
设置
}
公共字符串类别
{
得到;
设置
}
公共字符串IdCordinator
{
得到;
设置
}
}
}
您可以这样编写以获得所需的输出:
var lst = stdLst.GroupBy(s => s.IdCordinator)
.SelectMany(sg =>
sg.Where(s => !s.Category.Equals("S") || sg.Count(c => c.Category.Equals("S")) > 1)
.Select(student => student.Name))
.OrderBy(o => o);
希望这能解决你的问题
按如下方式更新构造函数:
public Students( string name, int age, string cat, string siCordinator )
{
this.Name = name;
this.Age = age;
this.Category = cat;
this.IdCordinator = siCordinator; // You were assiging same field to itself here, making it null
}
你想得到什么作为查询的输出结果,可以请你更清楚的输出应该是列表stNames=[“stud1”,“stud2”,“stud3”,“stud4”,“stud5”]这里的“stud6”应该在IdCordinator组下的类别“S”中,它只有一个记录PS:我已经更新了代码以显示正确的学生名称否仍然输出是stud1,stud2,stud3,研究4,研究5,研究6,研究7。。。Console.WriteLine(string.Join(“,”,(string[])lst.ToArray());。哪里不应该come@kvk95,再次回顾您的问题,您所问的是,您正在按“IdCordinator”属性和条件2对记录进行分组。“如果特定协调员的类别类型为“S”,且计数大于1,则包括在组中”。所以你的Stud7属于“I002”组,其计数为2,大于1,所以它显然包括在内。请不要发表自相矛盾的声明。确定你想要什么。我没有做任何条件声明,它说的是特定协调员&S属于协调员“I002”,在这种情况下,只有一个“S”在场。如果问题不清楚,我非常抱歉。@kvk95,我得到的是,您是通过IdCordinator进行分组的,您的I002组有2名学生stud3和stud7,因此将其包括在内,如果您的组只有1名学生且类别为“S”,则将不包括在内。我就是这么做的。请现在查看代码,我已经更新了。我想这就是你最终想要的。问题越清楚,你得到帮助的速度就越快。:)