Orderby in IQueryable Lambda Expressions

Go To StackoverFlow.com

1

I'm having trouble with following EF lambda expressions.

partial void StatusCallBackRequired_PreprocessQuery(ref IQueryable<PatientsTelephoneFollowupDetail> query)
{
    var newList = PatientsTelephoneFollowupDetails.OrderBy(x => x.Id).ToList();
    query = query.Where(p => p.PatientsMasterItem.PatientsTelephoneFollowupDetail.OrderByDescending(c => c.Id).FirstOrDefault(c => c.Status == "7") != null);
 }

The error is:

'Microsoft.LightSwitch.IOrderedDataServiceQueryable' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'Microsoft.LightSwitch.IOrderedDataServiceQueryable' could be found (are you missing a using directive or an assembly reference?)

I have already included system.linq namespace.

EDIT:(the results based on D Stanley code)

The sample data can be found below and I'm looking to retrieve the highlighted record enter image description here

However the current output is

enter image description here

Changing query to query.select returns only one record but it's is the wrong record.

2012-04-05 02:23
by user1213055
Did you rebuild it to make sure you aren't looking at stale compiler errors - M.Babcock 2012-04-05 02:32
Are you sure System.Linq has an extension method for IOrderedDataServiceQueryable? When I google IOrderedDataServiceQueryable, then only link that comes up is this SO question. I can't even research that type to see if Linq provides an extension method for it - Bob Horn 2012-04-05 03:20
Just out of interest why are you delcaring newList then not using it - Luke McGregor 2012-04-05 03:21
What is Microsoft.LightSwitch.IOrderedDataServiceQueryable? Does it extent IEnumerable? Or, does it have any extends method ToList? If does, you must using the namespace that contains the extends method - Kirin Yao 2012-04-05 04:08
I think the preprocessquery function only supports Iqueryable. Is there an alternative to implement same using IQueryable instead of IEnumerable - user1213055 2012-04-05 04:23


0

Couple of things to try:

cast to IEnumerable<T>:

var newList = ((IEnumerable<PatientsTelephoneFollowupDetail>)PatientsTelephoneFollowupDetails.OrderBy(x => x.Id)).ToList();

Initialize the list from the query result:

var newList = new List<PatientsTelephoneFollowupDetail>(PatientsTelephoneFollowupDetails.OrderBy(x => x.Id)));

EDIT Looks like LightSwitch doesn't implement IEnumerable<T>. Two more things to try:

Assuming it implements IEnumerable, use IEnumerable.Cast<T>():

var newList = new List<PatientsTelephoneFollowupDetail>(
    PatientsTelephoneFollowupDetails.OrderBy(x => x.Id)
        .Cast<PatientsTelephoneFollowupDetail>()
    );

brute force:

var newList = new List<PatientsTelephoneFollowupDetail>();
foreach(var item in PatientsTelephoneFollowupDetails.OrderBy(x => x.Id))
    newList.Add(item);
2012-04-05 03:50
by D Stanley
Tried 1st one no compile errors but app crashes when executing this query with following error Unable to cast object of type 'Microsoft.LightSwitch.ServerGenerated.Implementation.QueryImplementation1[LightSwitchApplication.PatientsTelephoneFollowupDetail]' to type 'System.Collections.Generic.IEnumerable1[LightSwitchApplication.PatientsTelephoneFollowupDetail]'.user1213055 2012-04-05 04:20
2nd one gives me following errors Error 1 The best overloaded method match for 'System.Collections.Generic.List<LightSwitchApplication.PatientsTelephoneFollowupDetail>.List(System.Collections.Generic.IEnumerable<LightSwitchApplication.PatientsTelephoneFollowupDetail>)' has some invalid arguments Error 2 Argument 1: cannot convert from 'Microsoft.LightSwitch.IOrderedDataServiceQueryable<LightSwitchApplication.PatientsTelephoneFollowupDetail>' to 'System.Collections.Generic.IEnumerable<LightSwitchApplication.PatientsTelephoneFollowupDetail>' user1213055 2012-04-05 04:20
I think the preprocessquery function only supports Iqueryable. I still don't understand the differences between EF,ADO.NET, LINQtoSQL et - user1213055 2012-04-05 04:21
IQueryable derives from IEnumerable, but apparently LightSwitch does not also implement IEnumerable. I've edited my answer to give you another couple of options - D Stanley 2012-04-05 04:38
IEnumerable.Cast() seems to work. However, the result is not as I expected. May be is it because of not using the list variable ? How can I apply the ordered new list into the query - user1213055 2012-04-05 06:05
I can't answer that without knowing more about your data and what you are expecting. How do you define the "right" record? Most recent? Then sort by date descending and take the FirstOrDefault(), for example - D Stanley 2012-04-05 13:58
I'm trying to find whether the most record in the table has status = 7. I'm ordering them by ID instead of date. The reason why I have to sort them because unfortunately lightswitch doesn't support lastordefault so I have to swap the order to find the most recent record. Please also refer to the screenshot above in original question. Hope it helps - user1213055 2012-04-05 22:00


0

You need to find the definition of this type: Microsoft.LightSwitch.IOrderedDataServiceQueryable

Once you've found it, confirm:

Does it implement IEnumerable<T>? If so, your code can't reach System.Linq.Enumerable. You say you are using System.Linq, so make sure your project is referencing System.Core.dll .

If not, does it implement IEnumerable? If so, you can write a foreach loop and add each item to a List<T> that you make.

If not, examine the properties. There is usually some way to get at the something you can write a loop against.

2012-04-05 03:52
by Amy B
I'm using LightSwitch and please refer to this post on msdn for more information - user1213055 2012-04-05 04:12
Ads