CS1929 error
Hi,
I am trying to run the LINQ statement below in LINQPad and I get the CS1929 error. The error comes from the mem2.RentalRate part (underlined in red in LINQPad). Could you please help to see how to resolve this error message. Thanks so much.
Error:
CS1929 'decimal' does not contain a definition for 'Max' and the best extension method overload 'Enumerable.Max(IEnumerable)' requires a receiver of type 'IEnumerable'
LINQ query:
var queryGroupMax =
from mem in Loans
group mem by mem.MemberID into memGroup
select new
{
Level = memGroup.Key,
HighestScore =
(from mem2 in memGroup
select mem2.RentalRate.Max())
};
I am trying to run the LINQ statement below in LINQPad and I get the CS1929 error. The error comes from the mem2.RentalRate part (underlined in red in LINQPad). Could you please help to see how to resolve this error message. Thanks so much.
Error:
CS1929 'decimal' does not contain a definition for 'Max' and the best extension method overload 'Enumerable.Max(IEnumerable)' requires a receiver of type 'IEnumerable'
LINQ query:
var queryGroupMax =
from mem in Loans
group mem by mem.MemberID into memGroup
select new
{
Level = memGroup.Key,
HighestScore =
(from mem2 in memGroup
select mem2.RentalRate.Max())
};
Comments
.Max() can only be applied to a collection of numbers and not to a single number.
mem2.RentalRate is a single number therefore you cannot select mem2.RentalRate.Max().
The expression "from mem2 in memGroup select mem2.RentalRate" evaluates to a collection of numbers which .Max() can be applied to.
So if you just move the most right parenthesis to the left, before .Max() You will get the desired result.
It can be simplified by using the .Max(selector) overload:
var bkQuery =
from lo in Loans
where (lo.DateReturn).Subtract(lo.DateDue).Days > 7
select lo;
bkQuery.Dump();
Example constant value in AddDays(): Example SqlMethods function: