-2

I have:

List<MyStruct> MyList = new List<MyStruct>();
// Here i do my logic and fill MyList

int max_price = ??? //How i can found max price in MyList


Struct MyStruct
{
public int Price;
public string Name;
}

P.S - Not foreach/for loop

I`m can found with foreach/for loop - but maybe List exist better method

Thanks

And how i Can do SUM for All Prices in List???

  • 2
    Just type your question in to your favorite web search engine. _"C# list find max value of property"_ or _"C# list sum value of property"_ will yield great results. See [Total sum for an object property value in a list using a lambda function](http://stackoverflow.com/questions/10316444/total-sum-for-an-object-property-value-in-a-list-using-a-lambda-function) for your second question. Also, your _"P.S - Not foreach/for loop"_ makes it look like you have the wrong idea of this site - it's not a code writing service. – CodeCaster Nov 07 '14 at 10:49

1 Answers1

4

You can use LINQ

int max_price = MyList.Max(ms => ms.Price)

It throws an exception if the list is empty, so you should handle that case separately.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939