Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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# 从列表中获取所需对象…;_C#_.net_Linq - Fatal编程技术网

C# 从列表中获取所需对象…;

C# 从列表中获取所需对象…;,c#,.net,linq,C#,.net,Linq,假设我有一个类“Person” public class Person { public string Name { get; set; } public int Age { get; set; } } 假设我有一个列表,其中包含15个人 List<Person> listOfPerson = new List<Person>(); List listoperson=new List(); 现在,我正在尝试使用最大年龄从列表中获取对象人员 仔细阅读,

假设我有一个类
“Person”

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
假设我有一个列表,其中包含15个人

List<Person> listOfPerson = new List<Person>();
List listoperson=new List();
现在,我正在尝试使用最大年龄从列表中获取对象人员

仔细阅读,我不仅仅需要最大年龄…… 但整个对象……因此我还可以访问年龄最大的人的姓名……

谢谢

Person oldest = listOfPerson.OrderByDescending(person => person.Age).First();
根据评论

假设我 说…所有和max在一起的人 年龄…(具有最大年龄的人员列表) 从给定的人员列表中选择)?谢谢

在这种情况下,找到最大年龄并对其进行过滤是值得的。有多种方法,但有一种简单的方法

int maxAge = listOfPerson.Max(person => person.Age);
var oldestPeople = listOfPerson.Where(person => person.Age == maxAge); // .ToList()
如果结果必须是列表而不是IEnumerable,请包括可选的
ToList()
扩展名

根据评论

假设我 说…所有和max在一起的人 年龄…(具有最大年龄的人员列表) 从给定的人员列表中选择)?谢谢

在这种情况下,找到最大年龄并对其进行过滤是值得的。有多种方法,但有一种简单的方法

int maxAge = listOfPerson.Max(person => person.Age);
var oldestPeople = listOfPerson.Where(person => person.Age == maxAge); // .ToList()

包括可选的
ToList()
扩展名,如果结果是一个列表而不是
IEnumerable
这一点很重要,那么如果列表很短,那么排序和选择第一个(如前所述)可能是最简单的

如果您有一个较长的列表,并且排序开始变慢(这可能不太可能),您可以编写自己的扩展方法来完成它(我使用MaxItem,因为我认为just Max会与现有的LINQ方法冲突,但我太懒了,无法找到)

publicstatictmaxitem(此IEnumerable列表,Func选择器){
var枚举器=list.GetEnumerator();
如果(!enumerator.MoveNext()){
//在空列表上返回null/default。可以选择抛出。
返回默认值(T);
}
T maxItem=枚举数.Current;
int maxValue=选择器(maxItem);
while(枚举数.MoveNext()){
变量项=枚举数。当前;
var值=选择器(项目);
如果(值>最大值){
最大值=最大值;
maxItem=项目;
}
}
返回maxItem;
}
或者,如果您需要返回所有最大项目:

public static IEnumerable<T> MaxItems<T>(this IEnumerable<T> list, Func<T, int> selector) {
    var enumerator = list.GetEnumerator();

    if (!enumerator.MoveNext()) {
        return Enumerable.Empty<T>();
    }

    var maxItem = enumerator.Current;
    List<T> maxItems = new List<T>() { maxItem };
    int maxValue = selector(maxItem);

    while (enumerator.MoveNext()) {
        var item = enumerator.Current;
        var value = selector(item);

        if (value > maxValue) {
            maxValue = value;
            maxItems = new List<T>() { item };
        } else if (value == maxValue) {
            maxItems.Add(item);
        }
    }

    return maxItems;
}
公共静态IEnumerable MaxItems(此IEnumerable列表,Func选择器){
var枚举器=list.GetEnumerator();
如果(!enumerator.MoveNext()){
返回可枚举的.Empty();
}
var maxItem=枚举数.Current;
List maxItems=新列表(){maxItem};
int maxValue=选择器(maxItem);
while(枚举数.MoveNext()){
变量项=枚举数。当前;
var值=选择器(项目);
如果(值>最大值){
最大值=最大值;
maxItems=new List(){item};
}else if(value==maxValue){
添加(项目);
}
}
归还物品;
}

如果列表较短,则排序和选择第一个(如前所述)可能是最简单的

如果您有一个较长的列表,并且排序开始变慢(这可能不太可能),您可以编写自己的扩展方法来完成它(我使用MaxItem,因为我认为just Max会与现有的LINQ方法冲突,但我太懒了,无法找到)

publicstatictmaxitem(此IEnumerable列表,Func选择器){
var枚举器=list.GetEnumerator();
如果(!enumerator.MoveNext()){
//在空列表上返回null/default。可以选择抛出。
返回默认值(T);
}
T maxItem=枚举数.Current;
int maxValue=选择器(maxItem);
while(枚举数.MoveNext()){
变量项=枚举数。当前;
var值=选择器(项目);
如果(值>最大值){
最大值=最大值;
maxItem=项目;
}
}
返回maxItem;
}
或者,如果您需要返回所有最大项目:

public static IEnumerable<T> MaxItems<T>(this IEnumerable<T> list, Func<T, int> selector) {
    var enumerator = list.GetEnumerator();

    if (!enumerator.MoveNext()) {
        return Enumerable.Empty<T>();
    }

    var maxItem = enumerator.Current;
    List<T> maxItems = new List<T>() { maxItem };
    int maxValue = selector(maxItem);

    while (enumerator.MoveNext()) {
        var item = enumerator.Current;
        var value = selector(item);

        if (value > maxValue) {
            maxValue = value;
            maxItems = new List<T>() { item };
        } else if (value == maxValue) {
            maxItems.Add(item);
        }
    }

    return maxItems;
}
公共静态IEnumerable MaxItems(此IEnumerable列表,Func选择器){
var枚举器=list.GetEnumerator();
如果(!enumerator.MoveNext()){
返回可枚举的.Empty();
}
var maxItem=枚举数.Current;
List maxItems=新列表(){maxItem};
int maxValue=选择器(maxItem);
while(枚举数.MoveNext()){
变量项=枚举数。当前;
var值=选择器(项目);
如果(值>最大值){
最大值=最大值;
maxItems=new List(){item};
}else if(value==maxValue){
添加(项目);
}
}
归还物品;
}

Ha,Ha,你用了“var”!再比我快五秒!同样的答案!瞬间滑倒。发生在星期五。@Anthony Pegram,,假设我说……所有年龄最大的人……(给定名单中年龄最大的人的名单)?谢谢…@Anthony,…是否可以获取包含给定列表中所有年龄的人的列表…@Anthony,…我获取了列表年龄列表=(从列表中的人选择person.Age).ToList();哈哈,你用了“var”!再比我快五秒!同样的答案!瞬间滑倒。发生在星期五。@Anthony Pegram,,假设我说……所有年龄最大的人……(给定名单中年龄最大的人的名单)?谢谢…@Anthony,…是否可以获取包含给定列表中所有年龄的人的列表…@Anthony,…我获取了列表年龄列表=(从列表中的人选择person.Age).ToList();