Reputation: 11951
I have two list of ids, both list have the same amount of items, they have the same ids, they are just not in order, example:
List 1 - (194, 195, 196, 197, 198, 199, 200)
List 2 - (194, 200, 198, 195, 197, 196, 199)
I have an if statement that compairs these ids:
if (cells[i].scheduleTaskID == taskid)
This works well for the first id because they match (194) but the other ids do not match, is there away I can say if taskid does not match cells[i].scheduleTaskID try task id later??
Here is are additional code:
int taskid = reader.GetInt32(0); (gets taskid from database)
List<CellModel> cells
and this is CellModel:
public class CellModel
{
public uint scheduleTaskID { get; set; }
public string task { get; set; }
public string baselineDate { get; set; }
public string scheduledDate { get; set; }
public string actualDate { get; set; }
public string finishedDate { get; set; }
public bool selected { get; set; }
public override string ToString()
{
return scheduleTaskID.ToString();
}
}
Upvotes: 1
Views: 97
Reputation: 6476
You can use LINQ over a collection to see if your value matches any of the values contained in a collection like this:
List<int> list1 = new List<int> { 194, 195, 196, 197, 198, 199, 200 }; //populate this from db
List<CellModel> cells;
if(list1.Any(id => id.Equals(cells[i].scheduleTaskID)))
If you have a huge list of id's in the database and you don't want to pull down the whole list to compare, you could also loop the reader.
while(reader.Read)
{
int taskid = reader.GetInt32(0);
if (cells[i].scheduleTaskID == taskid)
{
// take action and end reading
break;
}
}
Upvotes: 2
Reputation: 77926
Considering that both are type List<int>
; if your sole concern is that; numbers in your list is not in order then you can sort them both by calling the Sort()
function on list and then can loop through them to check content equality like below
List<int> list1 = new List<int> { 194, 195, 196, 197, 198, 199, 200 };
List<int> list2 = new List<int> { 194, 200, 198, 195, 197, 196, 199 };
list2.Sort();
list1.Sort();
for (int i = 0; i < list1.Count; i++)
{
if (list1[i] == list2[i])
{
//Your Own processing logic here
}
}
Though I am bit confused as to, what actually you are trying to compare here?
Upvotes: 0