LINQ - to retrrieve max value in LINQ - Asked By anbu n on 21-Mar-11 11:35 AM

var _last = (from _O_Pro in dt.AsEnumerable()
where ((_O_Pro["OA_Header_Id"].ToString() == _dr.OA_Header_Id.ToString()) && _O_Pro["OA_Header_Id"].ToString() == _pr.Productwithpartname.ToString())

select new { Process_Id = _O_Pro["Process_Id"] }).Max();


in above linq i can't able to get the max value
Ravinder Jamgotre replied to anbu n on 21-Mar-11 11:42 AM
Hi Anbu,

Adopt this solution using the following LINQ query statement, I have tested it in SQL server 2005, it works well.



Code Snippet

  var max_Query =
         (from tab1 in mydb.AnimalTag        
         select tab1.AnimalTagNumber).Max();


And an MSDN article from MS 

Vic S replied to anbu n on 21-Mar-11 02:11 PM
That should work unless you misspelled a table/field name.
When you say it doesn't work what is really happening? Are you getting a value returned but it is not the max, are you getting an error or are you getting nothing returned with no error?
anbu n replied to Vic S on 22-Mar-11 02:39 AM
i am getting no values & no errors
Vic S replied to anbu n on 22-Mar-11 09:08 AM
When you try it without the .Max, do you get any values?
Mash B replied to anbu n on 23-Mar-11 01:40 PM
In your linq query when you say select new , the return type will be anonymous , for your solution its not needed , you can remove new keyword and try as shown below

var _last = (from _O_Pro
in dt.AsEnumerable()
       where ((_O_Pro["OA_Header_Id"].ToString() ==  
      _dr.OA_Header_Id.ToString()) &&     
      _O_Pro["OA_Header_Id"].ToString() == _pr.Productwithpartname.ToString())
         select _O_Pro["Process_Id"]).Max();

Let me know !!!