Mike Eghtebas
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.ISingleR esult<spGe tDataResul t>' does not contain a definition for 'OrderBy' and the best extension method overload 'System.Linq.Queryable.Ord erBy<TSour ce,TKey>(S ystem.Linq .IQueryabl e<TSource> , System.Linq.Expressions.Ex pression<S ystem.Func <TSource,T Key>>)' has some invalid arguments
Line 5 again: .OrderBy(getField2)
Instance argument: cannot convert from 'System.Data.Linq.ISingleR esult<spGe tDataResul t>' to 'System.Linq.IQueryable<sp GetDataRes ult>'
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.
Line 5 in this code: .OrderBy(getField2) has the following error:
'System.Data.Linq.ISingleR
Line 5 again: .OrderBy(getField2)
Instance argument: cannot convert from 'System.Data.Linq.ISingleR
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);
}
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?
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?
It appears that your SP returns a single value only. Thus, I guess, there is nothing to order ..:
'System.Data.Linq.ISingleR esult<spGe tDataResul t>' does not contain a definition for 'OrderBy'
/gustav
'System.Data.Linq.ISingleR
/gustav
ASKER
Hi Gustav,
The SP returns multiple records. It is in use and this how I can tell it returns more than one item.
Mike
The SP returns multiple records. It is in use and this how I can tell it returns more than one item.
Mike
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
/gustav
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ListItem[] my_outputs = (wscgsContext.spGetData("M
.OrderBy(getField2)
Note you had left semicolon (;) in end. Test after removing it as -
ListItem[] my_outputs = (wscgsContext.spGetData("M
.OrderBy(getField2)
Share your results.