Home

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())
};

Comments

  • You are almost there :)

    .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()
    HighestScore =
    (from mem2 in memGroup
    select mem2.RentalRate).Max()
    You will get the desired result.

    It can be simplified by using the .Max(selector) overload:
    HighestScore = memGroup.Max(x => x.RentalRate)
  • nescafe, thanks a lot! Yes, your solution works. Thanks for the explanation of the solution too. It helped as I am quite new to LINQ.
  • BTW, I want to get the difference between two given dates in days. I tried the following query, but I got an error stating it does not contain a definition for 'Subtract'. Could you please suggest a method to get the difference between two days.

    var bkQuery =
    from lo in Loans
    where (lo.DateReturn).Subtract(lo.DateDue).Days > 7
    select lo;

    bkQuery.Dump();
  • edited October 2016
    Either use constant values in your datetime functions or use the SqlMethods mentioned in this MSDN article: https://msdn.microsoft.com/en-us/library/bb882657.aspx

    Example constant value in AddDays():
    where lo.DateReturn > lo.DateDue.AddDays(7) // use DateDue.Value if DateDue is Nullable
    Example SqlMethods function:
    where SqlMethods.DateDiffDay(lo.DateDue, lo.DateReturn) > 7
  • Thanks a lot, nescafe! It works.
Sign In or Register to comment.