Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.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# 获取列表<;SomeClass>;在.GroupBy()之后_C#_Select_Group By - Fatal编程技术网

C# 获取列表<;SomeClass>;在.GroupBy()之后

C# 获取列表<;SomeClass>;在.GroupBy()之后,c#,select,group-by,C#,Select,Group By,有人能告诉我如何使用GroupBy和Select来获取以下信息: public class BadClass{ public int id; //can be grouped by id public string name; //identical for the same Id public bool flag; //identical for the same Id public int bId; public string bName; p

有人能告诉我如何使用
GroupBy
Select
来获取以下信息:

public class BadClass{
    public int id; //can be grouped by id
    public string name; //identical for the same Id
    public bool flag; //identical for the same Id
    public int bId;
    public string bName;
    public int cId;
    public string cName;
}
这个:
列表
其中:

public class A {
    public int id;
    public string name;
    public bool flag;
    public List<B> bList;
}

public class B {
    public int id;
    public string name;
    public C c;
}

public class C {
    public int id;
    public int name;
}
但我不知道怎么做才对

更新:

var result = badClass
    .GroupBy(x => x.id)
    .Select(x => new A {
        id = x.Key,
        name = x.First().name,
        flag = x.First().flag,
        bList = x.ToList()
    });
var result = badClass
        .GroupBy(x => x.id)
        .Select(x => new A
        {
            id = x.Key,
            name = x.First().name,
            flag = x.First().flag,
            bList = badClass.Where(y => y.id == x.Key).Select(y =>
                new B { id = y.bId, name = y.bName, c = new C { id = y.cId, name = y.cName } }).ToList()
        });
这是
bList=x.ToList()中的错误我需要将名称
bId
更改为
id
e.t.c

回答:

var result = badClass
    .GroupBy(x => x.id)
    .Select(x => new A {
        id = x.Key,
        name = x.First().name,
        flag = x.First().flag,
        bList = x.ToList()
    });
var result = badClass
        .GroupBy(x => x.id)
        .Select(x => new A
        {
            id = x.Key,
            name = x.First().name,
            flag = x.First().flag,
            bList = badClass.Where(y => y.id == x.Key).Select(y =>
                new B { id = y.bId, name = y.bName, c = new C { id = y.cId, name = y.cName } }).ToList()
        });

只需添加一个.Select和ToList,它将包含您需要的所有项目

var result = badClass.GroupBy(x => x.id).Select(x => x.ToList());
这会让你得到你想要的


但老实说,我更喜欢一个
foreach
loop+
Dictionary
以提高可读性。

.Select(x=>x.Key)
应该包含您的分组,而
.Select(x=>x.ToList())
应该包含您在该组中的项目。组合您需要的内容。
var result=badClass.GroupBy(x=>x.id)。选择(x=>newa{id=x.Key,name=x.First().name,flag=x.First().flag,bList=x.ToList()})这里是
bList=x.ToList()中的错误我需要将名称
bId
更改为
id
e.t.c.@Caramiriel我需要重命名bId,bName,cId,cName,但不知道如何更改。我需要将其转换为
列表
,重命名
bId
bName
cName
我需要重命名bId,bName,cId,cName,但不知道如何更改。酷,谢谢!这就是我需要的!为什么要在
foreach
loop+
Dictionary
?它工作得更快吗?@DenisRoss对于小数据集来说,差异可以忽略不计。对于大数据集,字典更快。但主要原因是它的可读性。foreach对于其他ppl来说更容易理解。
var result = badClass
        .GroupBy(x => x.id)
        .Select(x => new A
        {
            id = x.Key,
            name = x.First().name,
            flag = x.First().flag,
            bList = badClass.Where(y => y.id == x.Key).Select(y =>
                new B { id = y.bId, name = y.bName, c = new C { id = y.cId, name = y.cName } }).ToList()
        });