Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Group and sort LINQ, c# asp

Posted on 2014-11-18
8
Medium Priority
?
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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 51

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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 34

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 51

Assisted Solution

by:Gustav Brock
Gustav Brock earned 1000 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 34

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 51

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 1000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

662 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