Click here to Skip to main content
15,112,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

In a generic class I want to sort my data from a webAPI.
I have a variable in that class that holds the default sort field name in the collection of properties.

What ever I try, the data does not get sorted.

My code:
C#
IOrderedEnumerable<T> data;
                if (!string.IsNullOrEmpty(DropDownListDataTextField))
                    data = dataservice.OrderBy(x => x.GetType().GetProperty(DropDownListDataTextField));
                else
                    data = dataservice as IOrderedEnumerable<T>;
                ddl.DataSource = data;
                ddl.DataTextField = OtherDataTextField ?? DropDownListDataTextField;
                ddl.DataValueField = "Id";
                ddl.DataBind();


When debugging the part
C#
x => x.GetType().GetProperty(DropDownListDataTextField)
leads to a NULL value

What I have tried:

How can I fix this problem? The fieldvalue ("Status") in DropDownListDataTextField is a property in the webAPI collection of data.
Posted
Updated 2-Feb-20 23:21pm

We can't tell, because we have no access to your data and that is what is controlling the assignment on both sides of the if statement.
I'd start by using the debugger to look at the content of dataservice and finding out exactly what it is, and what types it contains.
Then check which side if the if...else is executing, and either look really closely at the data content or the data type of dataservice.
If it's the else side, then I'd guess that whatever dataservice contains is either null or not an instance of a IOrderedEnumerable<T> (whatever class T happens to be at runtime).
If it's the if side then the content of the collection is the defining data.

Sorry, but we can't do any of that for you - we have no access at all to your code while it is running!
   
Comments
Herman<T>.Instance 3-Feb-20 3:28am
   
Griff, I know it is monday morning.

I did debug and I know it is the IF and not the ELSE part it is running.....
OriginalGriff 3-Feb-20 3:33am
   
So what did the debugger show was in the data?
Herman<T>.Instance 3-Feb-20 3:48am
   
NULL
but the column does exist.
I added .GetValue(dataservice) and then I get the error:
'this' parameter not instance of type

the var Data has the error: Object does not match target type.
OriginalGriff 3-Feb-20 3:54am
   
Yes, but what type is it? And what string does DropDownListDataTextField contain?
If GetType returns a type that doesn't have a property matching the name you look for, then the sort will just be comparing NULL values.
Herman<T>.Instance 3-Feb-20 3:59am
   
<t> is the webAPI object ModelStatu (is table via EF in webAPI Controller).
DropDownListDataTextField holds the name to display in a dropdownlist and has the value "Status"
OriginalGriff 3-Feb-20 4:11am
   
Hmmm. So what is GetType returning?
Herman<T>.Instance 3-Feb-20 4:15am
   
{System.Linq.OrderedEnumerable<marinmodelservice.models.modelstatu, system.type="">}

dataservice.OrderBy(x => x.GetType().GetProperty(DropDownListDataTextField)) leads to an unordered IeNumerable<t>
lmoelleb 3-Feb-20 4:37am
   
Hint on how to debug: Place the breakpoint inside the lambda (so the call to x.GetType()...)
Once it hits, try running x.GetType().GetProperty(...) in the intermediate window of Visual studio. Observe the data it returns. It will not be what you expect....
Herman<T>.Instance 3-Feb-20 6:26am
   
I did that, but the issue is solved. Thanks
OriginalGriff 3-Feb-20 4:52am
   
And System.Linq.OrderedEnumerable doesn't have a Status property ...
So what did you expect dataservice to iterate over?

And ... did you mean to write

x.GetType().GetProperty(DropDownListDataTextField).GetValue(x)
Herman<T>.Instance 3-Feb-20 5:13am
   
Thanks Mate!

I told you it is monday morning.....
OriginalGriff 3-Feb-20 6:06am
   
It took me long enough to notice that myself! :laugh:
In a Short: your class have to implement IComparable interface. See: IComparable Interface (System) | Microsoft Docs[^]
   
Comments
Herman<T>.Instance 3-Feb-20 5:14am
   
I did read that solution to and found
https://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object as simple answer. The option Griff gave has minimal code.
Thanks for your help!
Maciej Los 3-Feb-20 5:50am
   
You're very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900