简体   繁体   English

LINQ 查询以从对象列表中返回不同的字段值

[英]LINQ query to return distinct field values from list of objects

class obj
{
    int typeId; //10 types  0-9 
    string uniqueString; //this is unique
}

Assume there is a list with 100 elements of objs, but only 10 unique typeIDs.假设有一个包含 100 个 objs 元素的列表,但只有 10 个唯一的 typeID。

Is it possible to write a LINQ query that returns the 10 unique ints from the list of objs?是否可以编写一个 LINQ 查询来返回 objs 列表中的 10 个唯一整数?

class obj
{
    int typeId; //10 types  0-9 
    string uniqueString; //this is unique
}

Assume there is list with 100 elements of obj, but only 10 unique typeIDs.假设有一个包含 100 个 obj 元素的列表,但只有 10 个唯一的 typeID。
Is it possible to do write a LINQ query return the 10 unique ints from the list of objs?是否可以编写一个 LINQ 查询返回 objs 列表中的 10 个唯一整数?

class obj
{
    int typeId; //10 types  0-9 
    string uniqueString; //this is unique
}

Assume there is list with 100 elements of obj, but only 10 unique typeIDs.假设有一个包含 100 个 obj 元素的列表,但只有 10 个唯一的 typeID。
Is it possible to do write a LINQ query return the 10 unique ints from the list of objs?是否可以编写一个 LINQ 查询返回 objs 列表中的 10 个唯一整数?

class obj
{
    int typeId; //10 types  0-9 
    string uniqueString; //this is unique
}

Assume there is list with 100 elements of obj, but only 10 unique typeIDs.假设有一个包含 100 个 obj 元素的列表,但只有 10 个唯一的 typeID。
Is it possible to do write a LINQ query return the 10 unique ints from the list of objs?是否可以编写一个 LINQ 查询返回 objs 列表中的 10 个唯一整数?

class obj
{
    int typeId; //10 types  0-9 
    string uniqueString; //this is unique
}

Assume there is list with 100 elements of obj, but only 10 unique typeIDs.假设有一个包含 100 个 obj 元素的列表,但只有 10 个唯一的 typeID。
Is it possible to do write a LINQ query return the 10 unique ints from the list of objs?是否可以编写一个 LINQ 查询返回 objs 列表中的 10 个唯一整数?

class obj
{
    int typeId; //10 types  0-9 
    string uniqueString; //this is unique
}

Assume there is list with 100 elements of obj, but only 10 unique typeIDs.假设有一个包含 100 个 obj 元素的列表,但只有 10 个唯一的 typeID。
Is it possible to do write a LINQ query return the 10 unique ints from the list of objs?是否可以编写一个 LINQ 查询返回 objs 列表中的 10 个唯一整数?

class obj
{
    int typeId; //10 types  0-9 
    string uniqueString; //this is unique
}

Assume there is list with 100 elements of obj, but only 10 unique typeIDs.假设有一个包含 100 个 obj 元素的列表,但只有 10 个唯一的 typeID。
Is it possible to do write a LINQ query return the 10 unique ints from the list of objs?是否可以编写一个 LINQ 查询返回 objs 列表中的 10 个唯一整数?

class obj
{
    int typeId; //10 types  0-9 
    string uniqueString; //this is unique
}

Assume there is list with 100 elements of obj, but only 10 unique typeIDs.假设有一个包含 100 个 obj 元素的列表,但只有 10 个唯一的 typeID。
Is it possible to do write a LINQ query return the 10 unique ints from the list of objs?是否可以编写一个 LINQ 查询返回 objs 列表中的 10 个唯一整数?

If you want to get the distinct objects themselves, use a linq query like this:如果您想自己获取不同的对象,请使用 linq 查询,如下所示:

List<Person> distinctPersons = allPersons.GroupBy(p => new { p.Email } ).Select(g => g.First()).ToList();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM