Click here to Skip to main content
15,917,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii ,


XML
List<mylib.MyRole> lst = mylib.MyRole.GetAccessSupportRoleDetailList(compId);
                    int? Id = lst.Where(m => m.IsForMasterAdmin == isForMasterAdmin && m.CompanyId == compId).LastOrDefault().AccessLevelId;



This is what i am trying to do ... where ... I want to take last value as per the metion condition in lastordefault bracket . but its not working

Can you please also suggest me how this things actually work .. I mean ..

lst.where(condition).lastordefault();

or lst.lastordefault(condition)

are the above two things are work exactly same .. I am bit confuse in this type scenrios ..
Posted
Updated 5-Jun-14 1:32am
v4

XML
List<mylib.MyRole> lst = mylib.MyRole.GetAccessSupportRoleDetailList(compId);
                    int? Id = (lst.Where(m => m.IsForMasterAdmin == isForMasterAdmin && m.CompanyId == compId).AccessLevelId).LastOrDefault();


Try this it may help you.
 
Share this answer
 
Comments
Torakami 5-Jun-14 7:50am    
AccessLevelId is not getting
Pravuprasad 6-Jun-14 2:45am    
i'm not getting you ,your error is at accesslevelid/lastordefault() ?
That's not a safe thing to do: the default value for any reference type (which I assume MyRole is) is null - so LastOrDefault will return null if there are no matching values.
As a result,
C#
.LastOrDefault().AccessLevelId;
will cause your program to crash with an "null reference exception"

In terms of execution, the two are probably equivalent: Where will return an IEnumerable which the parameterless version of LastOrDefault will extract or the last value, if any, and the parameterised version with do the equivalent of the Where internally. If may be slightly more efficient to use the parameterised version, but I suspect that would depend on the exact make up of the input IEnumerable. It'd be a PITA to do performance testing on though, given that they are deferred execution queries anyway...

I'd go with the first version - I find it easier to read! :laugh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900