0

I have an Linq query like this:

var result = from c in displayedEmployees select new[] { c.Name, c.Code, c.Rank.ToString(), c.Id.ToString(),c.Rank. };

If i want to select the min and max of any column for example Rank along with this like:

var result = from c in displayedEmployees select new[] { c.Name, c.Code, c.Rank.ToString(), c.Id.ToString(),min(c.Rank) ,max(c.Rank)};

how can I do this?

1 Answer 1

1

Select actually performs a transformation of every item (displayedEmployee), so ends up with the same number of items as you started with. Min and max are reductions, they reduce the list into a scalar.

After the "select" keyword is a function that only recieves the current item (c in your case). So there is no way to get the max there and you don't want every item in result to repeat the same maximum value.

You should compute the minimum and maximum separately.

After:

var result = from c in displayedEmployees select { c.Name, c.Code, c.Rank };

You could do something like this:

var min = result.Min(x => x.Rank);
var max = result.Max(x => x.Rank);

See more samples here (LINQ 101): http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

Sign up to request clarification or add additional context in comments.

2 Comments

if select to array select new[] then after selecting don't have access to field by name, so when you try ` result.Min(x => x.Rank);` will be error
Indeed, the first line in the question is not correct. I'll change my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.