Reputation: 878
I've made a list and, in certain circumstances, the list is populated. Simple. However, when I try to filter the list based on a dropdown selection ddlLeadCounty.SelectedItem.Value
, the count comes back as 0. This is definitely not true as I have manually checked that both the value in the dropdown and the value pulled into the list are the same.
Could any of you wise people see where it may be going wrong? I have tried both of the following methods to the same result.
c# .net code behind
myList = myList.FindAll(delegate(Partner part)
{
return part.RegionId.Equals(ddlLeadCounty.SelectedItem.Value);
});
OR
myList = myList.Where(c => c.RegionId.Equals(ddlLeadCounty.SelectedItem.Value)).ToList();
Partner List:
public class Partner
{
public int LeadOppCount;
public string Guid;
public int RegionId;
public Partner(int LeadOppCount, string Guid, int RegionId)
{
this.LeadOppCount = LeadOppCount;
this.Guid = Guid;
this.RegionId = RegionId;
}
}
Dropdown list example:
<asp:ListItem value="100000004">Berkshire</asp:ListItem>
In my tests, at least 1 list item definitely has a regionId of 100000004.
Many thanks in advance.
Upvotes: 0
Views: 64
Reputation: 4680
The problem is that SelectedIndex.Value is a string value not an int so you will have to parse it. The following line should work:
var myListFiltered = myList.Where(c => c.RegionId.Equals(int.Parse(ddlLeadCounty.SelectedItem.Value))).ToList();
Upvotes: 0
Reputation: 62256
Considering that you're I see RegionId
, I suppose it's a integer
, so write something like this a pseudocode:
int valueSelected = (int)ddlLeadCounty.SelectedItem.Value;
myList.FindAll(x=>x.RegionId == valueSelected );
In other words do not use Equals
, for possibly boxed value in SelectedValue
, but use concrete type.
Should work for you.
Upvotes: 2