-1

Couldn't getting any records.Throwing an error :

An exception of type 'System.NullReferenceException' occurred in DAL.dll but was not handled in user code Additional information: Object reference not set to an instance of an object.

Considering the following records:

Id          No            createdDate    Extension
 --------------------------------------------------
   1           9000          1990           10
   2           9000          1990           11
   3           9000          2000           12
   4           9849          2001           1
   5           9849          2002           2 
   6           9700          2010           4

var res = from cust in Customers
                      group cust by cust.No
                        into groups
                       select groups.OrderByDescending(p => p.Id).FirstOrDefault();

where as this same concept applying in Controller action methode like below.Then not getting any null error, getting records as per my requirement.

var result = from refC in _objRefCustomerBS.GetAllRefCustomer()
                         group refC by refC.MobileNo into grp
                         select grp.OrderByDescending(g => g.cuRefID).FirstOrDefault();

I want to group by based on the No field and sort by Id and get all fields from the last record of group similar to these records:

Id          No            createdDate    Extension
 -------------------------------------------------  
   3           9000          2000           12   
   5           9849          2002           2 
   6           9700          2010           4
abbas ahmed
  • 283
  • 2
  • 13

1 Answers1

1

The only problem I see in your query is that you assign cust as the variable name for items in Customers but then you group element which I'm not even sure what is as it's not declared within the code snippet you provided.

So, fix the naming and you should be good to go:

var res = from cust in Customers
        group cust by cust.No
        into groups
        select groups.OrderByDescending(p => p.Id).FirstOrDefault();
Imantas
  • 1,621
  • 13
  • 19