Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

Group and sort LINQ, c# asp

spGetData PROC returns few fields. The code below attempts to sort each separately before putting them in their respective list boxes. I have 2 errors:

Line 5 in this code: .OrderBy(getField2) has the following error:
'System.Data.Linq.ISingleResult<spGetDataResult>' does not contain a definition for 'OrderBy' and the best extension method overload 'System.Linq.Queryable.OrderBy<TSource,TKey>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,TKey>>)' has some invalid arguments      


Line 5 again: .OrderBy(getField2)
Instance argument: cannot convert from 'System.Data.Linq.ISingleResult<spGetDataResult>' to 'System.Linq.IQueryable<spGetDataResult>'      

Question: How can I correct these two errors?

A working sample using a table instead of a stored procedure included below for your inspection. This sample does exactly the same task but using a table.
               lb.Items.Clear();
               ParameterExpression o = Expression.Parameter(typeof(spGetDataResult));
               var getField2 = Expression.Lambda<Func<spGetDataResult, string>>(Expression.Property(o, fieldName), o);
               ListItem[] my_outputs = (wscgsContext.spGetData("Mike");
                                    .OrderBy(getField2)
                                    .GroupBy(getField2)
                                    .Select(theGroup => theGroup.FirstOrDefault())
                                    .Select(getField2)
                                    .Select(field => new ListItem(field))).ToArray();
               lb.Items.AddRange(my_outputs);

// The sample code below does the same thing successfully using table called Serach vs a PROC above:
private void LoadListBoxes(ListBox lb)
    {
           lb.Items.Clear();
           string fieldName = lb.ID.Substring(3);

               ParameterExpression o = Expression.Parameter(typeof(Search));
               var getField = Expression.Lambda<Func<Search, string>>(Expression.Property(o, fieldName), o);
               ListItem[] outputs = (wscgsContext.Searches
                                    .OrderBy(getField)
                                    .GroupBy(getField)
                                    .Select(theGroup => theGroup.FirstOrDefault())
                                    .Select(getField)
                                    .Select(field => new ListItem(field))).ToArray();
               lb.Items.AddRange(outputs);
}

Open in new window

Avatar of Vikram Singh Saini
Vikram Singh Saini
Flag of India image

Your code has typo mistake -

ListItem[] my_outputs = (wscgsContext.spGetData("Mike");
                                    .OrderBy(getField2)

Note you had left semicolon (;) in end. Test after removing it as -

ListItem[] my_outputs = (wscgsContext.spGetData("Mike")
                                    .OrderBy(getField2)

Share your results.
Avatar of Mike Eghtebas

ASKER

Vikram,

I must have added the ; by mistake when I was posting the question. I didn't have ; in my code in the application.

So, the question remains: How can I correct these two errors?
Avatar of Gustav Brock
It appears that your SP returns a single value only. Thus, I guess, there is nothing to order ..:

'System.Data.Linq.ISingleResult<spGetDataResult>' does not contain a definition for 'OrderBy'

/gustav
Hi Gustav,

The SP returns multiple records. It is in use and this how I can tell it returns more than one item.

Mike
SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Basically, This code returns items expected. I want it to display sorted. If you could revise the submitted code to do this, it will be great.
I can't, sorry. I have no SP to test with.

/gustav
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial