简体   繁体   English

如何从对象列表中获取属性,该对象是具有最低数值的字符串

[英]How to get the property from a list of object that is a string with the lowest numeric value

Say I have a list of people: 说我有一个人名单:

Var people = new List<Person>();

Where 哪里

Public class Person {
Public string ReferenceString { get; set; }
}

The reference string is a string of digits so I might have in my list; 引用字符串是一串数字,所以我可能在我的列表中;

Var person1 = new Person { ReferenceString = "12" };
Var person2  = new Person { ReferenceString = "11" };
Var person3 = new Person { ReferenceString = "14" };

What I want to do is somehow get back the reference string with the lowest numeric value which in this case is "11". 我想要做的是以某种方式取回具有最低数值的引用字符串,在本例中为“11”。 So I'm guessing it will need to be converted to an int somewhere on the line and was working along the lines of something like: 所以我猜它需要在线路的某个地方转换成一个int,并且正在按照以下方式工作:

people.Where(x => Int32.Parse(x.ReferenceString));

Not sure how to do the comparison though. 不知道怎么做比较。

You need to convert it to an int, order by that value and take the first(lowest): 您需要将其转换为int,按该值排序并取第一个(最低):

Person lowPerson = people.OrderBy(p => int.Parse(p.ReferenceString)).First();

If you only want the lowest value: 如果您只想要最低值:

int lowest = people.Min(p => int.Parse(p.ReferenceString));

But you should consider to store it as int in the first place. 但是你应该考虑首先将它存储为int。

Your code will give error because ReferenceString is string and you are assigning integer value to it. 您的代码将给出错误,因为ReferenceString是字符串,您正在为其分配整数值。

Since you said you cannot change datatype you can do 既然你说你不能改变数据类型就可以了

Var person1 = new Person { ReferenceString = "12" };
Var person2  = new Person { ReferenceString = '11" };
Var person3 = new Person { ReferenceString = "14" };

In that case use 在那种情况下使用

var min = people.Min(x => Convert.ToInt32(x.ReferenceString));

In case you want to find out which pesons have min ReferenceString, you can do. 如果你想找出哪些pesons有最小的ReferenceString,你可以做。

var result = people.Where(x => x.ReferenceString == min.ToString());

I would make ReferenceString as ReferenceInteger if it is storing numbers. 如果它存储数字,我会将ReferenceString作为ReferenceInteger Then, you can use Min method and get the records. 然后,您可以使用Min方法并获取记录。 Like this: 像这样:

var person1 = new Person { ReferenceString = 12 };
            var person2 = new Person { ReferenceString = 11 };
            var person3 = new Person { ReferenceString = 14 };

            var people = new List<Person>();
            people.Add(person1);
            people.Add(person3);
            people.Add(person2);

            var returnValues = people.Where(x => x.ReferenceString == people.Min(y => y.ReferenceString));

Here ReferenceString is of type integer. 这里ReferenceString的类型是整数。 Consider renaming the property accordingly. 考虑相应地重命名属性。

您可以使用MoreLinq的MinBy方法来查找具有最小值的人,而无需对所有人进行排序,如果您有多个人,则效率低下。

Person lowPerson = people.MinBy(p => int.Parse(p.ReferenceString));

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

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