Question

Lets say i have a List like this.

private List<TestClass> test()
{
    List<TestClass> tcList = new List<TestClass>();
    tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 2, prop3 = 3 });
    tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 4, prop3 = 5 });
    tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 2, prop3 = 6 });

    return tcList;

}

What i want to do is, i want to return all the elements which have ModulePosition = 1 and TopBotData = 2. I would also need the count of it satisfying the given condition. Here in this case it will be 2. Without using LINQ as i am using .net 2.0

Was it helpful?

Solution

You could wrap it up in a method and just yield return the results that match your criteria

public IEnumerable<TestClass> GetTests(List<TestClass> tests)
{
   foreach(var v in tests){
      if(v.ModulePosition == 1 && v.TopBotData == 2)
         yield return v;
   }
}

And Then

List<TestClass> tcList = new List<TestClass>();
tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 2, prop3 = 3 });
tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 4, prop3 = 5 });
tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 2, prop3 = 6 });

var results = new List<TestClass>(GetTests(tcList));
var count = results.Count;

OTHER TIPS

public int Count(List<TestClass> tests)
{
   int counter=0; 
   foreach(var v in tests){
      if(v.ModulePosition == 1 && v.topBotData == 2)
         counter++;
   }
    return counter;
}

you do this. Inside the if add any condition that you want

        for (int i = 0; i < tcList.Count; i++)
        {
            if (tcList[i].TopBotData == 2 && tcList[i].ModulePosition == 1)
            {
                result.Add(tcList[i]);
            }
        }

        return result;

you do this. Inside the if add any condition that you want.

In order to know the number of element just do result.Count

        for (int i = 0; i < tcList.Count; i++)
        {
            if (tcList[i].TopBotData == 2 && tcList[i].ModulePosition == 1)
            {
                result.Add(tcList[i]);
            }
        }

        return result;

I agree with Eoin's answer, but I'd do a more generic method, such as

private List<TestClass> GetByModuleAndTopBot(List<TestClass> list, int modulePosition, int topBotData)
{
    List<TestClass> result = new List<TestClass>();
    foreach (TestClass test in list)
    {
        if ((test.ModulePosition == modulePosition) &&
            (test.TopBotData == topBotData))
            result.Add(test);
    }
    return result;
}

therefore you can get the result you want by calling this method as follows:

GetByModuleAndTopBot(tcList, 1, 2);

and count it with .Count, as its return type is a List<>.

List<T> has a FindAll method that is almost identical to LINQ's Where:

return tcList.FindAll(
  delegate(TestClass x) { return x.ModulePosition == 1 && x.topBotData == 2; });

In newer versions of .NET, I'd recommend LINQ and lambda expressions, but for .NET 2.0, the above is probably the most concise way to do what you want (and therefore, IMHO, probably a good way).

You could also use a predicate:

private static bool MyFilter(TestClass item)
{
  return (item.ModulePosition) == 1 && (item.TopBotData == 2);
}
private static void Example()
{
  List<TestClass> exampleList = test();
  List<TestClass> sortedList = exampleList.FindAll(MyFilter);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top