I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

Linq query to consider even records and skip odd record

Below is my model :

public class MyData
{
    public int RegionId { get; set; }
    public string Region { get; set; }
    public List<Test> Tests { get; set; }
}

public class Test
{
    public int? TestId { get; set; }
    public List<SubTest> SubTests { get; set; }
}

Below is my method :

private void MyMethod(int testId, int lastTestId)
{
    var list = new List<MyData>();//contains some records
    if (testId == 0)
    {
        var test1 = list[0].Tests[0];
        var test2 = list[0].Tests[1];
        var regionIds = new List<int>();
        int counter = 0;
        foreach (var item in list)
        {
            counter = 0;
            foreach (var test in item.Tests)
            {
                if (test.SubTests.Count != 0 && counter % 2 != 0)
                    regionIds.Add(item.RegionId);

                counter++;
            }
        }
    }
    else
    {
        var test1 = list[0].Tests[0];
        var regionIds = new List<int>();
        foreach (var item in list)
        {
            counter = 0;
            foreach (var test in item.Tests)
            {
                if (test.SubTests.Count != 0)
                    regionIds.Add(item.RegionId);

                counter++;
            }
        }
    }
}

In the above method when testId will be 0, then I will have 2 records in Tests and when TestId > 0 then I will have only 1 record in Tests.

Now I am trying to find those region ids for which there is no SubTests(SubTest.Count=0) for a test, but when I will have 2 test then I want to consider last test and count subTest for that last test.

My code is working fine, but I have some code duplication, which I want to simplify.

Instead of loop I would prefer Linq, but as I was not knowing that how to do this with linq, I used a for loop.

Update:

Input :

[0] : Region = "abc"
      RegionId = 1
      "TestList": 
       [
         {
           TestId : 100,
           SubTests  : //contains some records
         },
         {
           TestId : 101,
           SubTests  : //contains some records
         },
       ],
[1] : Region = "Pqr",
      RegionId = 2
      "TestList": 
       [
         {
           TestId : 100,
           SubTests  : //contains some records
         },
         {
           TestId : 101,
           SubTests  : //No records i.e count = 0
         },
       ],
[2] : Region = "Lmn",
      RegionId = 3
      "TestList": 
       [
         {
           TestId : 100,
           SubTests  : //No records i.e count = 0
         },
         {
           TestId : 101,
           SubTests  : //No records i.e count = 0
         }
       ]

Expected Output :

[2,3]

How can I simplify this process?

Upvotes: 1

Views: 430

Answers (2)

ASh
ASh

Reputation: 35720

List<MyData> list; 
...
int[] ids = list.Where(x => x.Tests.Last().SubTests.Count == 0)
                .Select(x => x.RegionId)
                .ToArray();

expression in Where methods can be explained as "last test in Tests collection doesn't have subtests".

after we found all requiered tests from list we get their RegionId with Select method and save in array.

Upvotes: 1

Salvador Guerrero
Salvador Guerrero

Reputation: 255

Linq's Select and Where methods have an override where the lambda expression includes both the object and its index

https://msdn.microsoft.com/es-es/library/bb549418(v=vs.110).aspx

list.Where((item,index) => index % 2 == 0)

Here's a fiddle https://dotnetfiddle.net/GZippb

Upvotes: 2

Related Questions