Solved

Group and sort LINQ, c# asp

Posted on 2014-11-18
8
327 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

12 Experts available now in Live!

Get 1:1 Help Now