Solved

Group and sort LINQ, c# asp

Posted on 2014-11-18
8
336 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

919 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

19 Experts available now in Live!

Get 1:1 Help Now