Solved

Group and sort LINQ, c# asp

Posted on 2014-11-18
8
341 Views
Last Modified: 2016-02-15
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

0
Comment
Question by:Mike Eghtebas
8 Comments
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 40451491
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.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40451637
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?
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40459322
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
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40459812
Hi Gustav,

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

Mike
0
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 250 total points
ID: 40460083
But maybe Linq doesn't know that.

ISingleResult inherits IEnumerable and has an extension method called ToArray:

ISingleResultValue.Select(x => x.StringProperty).ToArray();
 
However, I don't use SPs this way so I cannot confirm.

/gustav
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40460109
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.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40461632
I can't, sorry. I have no SP to test with.

/gustav
0
 
LVL 11

Accepted Solution

by:
louisfr earned 250 total points
ID: 40464710
ISingleResultSet does not implement IQueryable. You cannot use an expression as parameter to OrderBy. Instead, pass a delegate by compiling the expression:
var getField2 = Expression.Lambda<Func<spGetDataResult, string>>(Expression.Property(o, fieldName), o).Compile();
ListItem[] my_outputs = (wscgsContext.spGetData("Mike")
                     .OrderBy(getField2)
                     .GroupBy(getField2)
                     .Select(theGroup => theGroup.FirstOrDefault())
                     .Select(getField2)
                     .Select(field => new ListItem(field))).ToArray();

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now