1. Dim InboundItems() As EmpClass = GetDataFromTheBLMethod()
2. Dim InboundItemsInfo As System.Reflection.PropertyInfo = GetType(EmpClass).GetProperty(CurrentSortExpression)
3. InboundItems = InboundItems.OrderByDescending(Function(InboundItem) InboundItemsInfo.GetValue(InboundItemsInfo, Nothing)).ToArray()
and I am getting "Object does not match target type" error at line 3. Am I missing some thing?
The SortExpression doesn't allow you to use a subproperty, i.e. Client.Name (where Client is a property of the object being used), so you need a way of making the Client.Name 'appear' as if it belongs to the containing object. As I said, create a property
called ClientName and then in the getter pass the value of Client.Name. For example:
public string ClientName
{
get
{
return Client.Name;
}
}
lusty_learne...
Participant
782 Points
390 Posts
Linq need help
Feb 15, 2012 03:56 PM|LINK
Hi,
I have the below code.
and I am getting "Object does not match target type" error at line 3. Am I missing some thing?
Thanks in advance,
L
stevenbey
All-Star
16526 Points
3378 Posts
Re: Linq need help
Feb 15, 2012 04:07 PM|LINK
You're passing InboundItemsInfo, but should be passing InboundItem, into the GetValue method.
http://stevenbey.com
Recursion: see Recursion
lusty_learne...
Participant
782 Points
390 Posts
Re: Linq need help
Feb 15, 2012 04:42 PM|LINK
Steve, Yes I over looked it. It works. I am sorting the array because I trying to provide sorting to my gridview.
I have a template field like this. Client is a class available as a property in the main class.
<asp:TemplateField HeaderText="Client Name" SortExpression="Client.Name"> <ItemTemplate> <asp:Label ID="ClientNameLabel" Text='<%#Eval("Client.Name") %>' runat="server" /> </ItemTemplate> </asp:TemplateField>and I am getting the null refrence exception. Am I missing some thing here though?
But it works as expected and it sorts if for the template field (directly avialable in the custom class)
<asp:TemplateField HeaderText="Loaded Date" SortExpression="LoadedDate"> <ItemTemplate> <asp:Label ID="LoadedDateLabel" Text='<%#Eval("LoadedDate") %>' runat="server" /> </ItemTemplate> </asp:TemplateField>
Thanks.
stevenbey
All-Star
16526 Points
3378 Posts
Re: Linq need help
Feb 15, 2012 05:11 PM|LINK
This is invalid notation. I suggest creating a property called ClientName that defines a getter, which returns the Client.Name value.
http://stevenbey.com
Recursion: see Recursion
lusty_learne...
Participant
782 Points
390 Posts
Re: Linq need help
Feb 15, 2012 10:51 PM|LINK
Steve, It is not going any where I am not sure if I am thinking it right. After creating a property how do we link it up to the gridview.
Can you please help me with a sample code snippet that can help me put things together?
Thanks in advance.
stevenbey
All-Star
16526 Points
3378 Posts
Re: Linq need help
Feb 16, 2012 09:28 AM|LINK
The SortExpression doesn't allow you to use a subproperty, i.e. Client.Name (where Client is a property of the object being used), so you need a way of making the Client.Name 'appear' as if it belongs to the containing object. As I said, create a property called ClientName and then in the getter pass the value of Client.Name. For example:
public string ClientName { get { return Client.Name; } }http://stevenbey.com
Recursion: see Recursion
Mastan Oli
Contributor
5088 Points
998 Posts
Re: Linq need help
Feb 16, 2012 10:13 AM|LINK
my advice that assign values into the column name like
InboundItems = (From InboundItem In InboundItems _ Order By InboundItem.GetValue(InboundItemsInfo, Nothing) Descending _ Select New With { _ Key .MyColumn1 = a.COLUMN1, _ Key .MyColumn2 = a.myColumn2, _ Key .Name = a.Name, _ Key .ClientName = a.Client.Name}).ToArray()Here, I added some dummy columns as MyColumn1, MyColumn2, Name
playingOOPS | மெய்ப்பொருள் காண்பதறிவு
Mark as Answer If you find helpful