| A | B | C |
| Naresh | Suresh | 6 |
| Ramesh | Rupesh | 6 |
| xyz | abc | 10 |
| pqr | asdd | 6 |
| asdsd | rewe | 12 |
| wesd | sfyere | 6 |
| sdyed | hesyd | 12 |
| ghedg | sged | 12 |
How to write linq query in c# to get the first sequence of 'C' column with 6 and immidiate value is 12.
From the above table expected result should be
| pqr | asdd | 6 |
Thanks in advance.

Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 16, 2023, 1:19 AM EST
Hello Naresh,
Try below code:
public class test { public string A { get; set; } public string B { get; set; } public int C { get; set; } }Inside some method EX: ABC()
List collection = new List(); collection.Add(new test() { A = "Naresh", B = "Suresh", C = 6 }); collection.Add(new test() { A = "Ramesh", B = "Rupesh", C = 6 }); collection.Add(new test() { A = "xyz", B = "abc", C = 10 }); collection.Add(new test() { A = "pqr", B = "asdd", C = 6 }); collection.Add(new test() { A = "asdsd", B = "rewe", C = 12 }); collection.Add(new test() { A = "wesd", B = "sfyere", C = 6 }); collection.Add(new test() { A = "sdyed", B = "hesyd", C = 12 }); collection.Add(new test() { A = "ghedg", B = "sged", C = 12 }); var result = collection.Where((item, index) => item.C == 6 && index < collection.Count - 1 && collection[index + 1].C == 12) .FirstOrDefault(); if (result != null) { Console.WriteLine(result.A + "\t" + result.B + "\t" + result.C); }Thanks
Naimish Makwana
Guest User140 reputationPosted Aug 8, 2023, 1:29 AM EDT
Thanks
Cr BhargaviRank #420 · 5.9K reputationPosted Aug 8, 2023, 1:34 AM EDT
using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { List> data = new List> { Tuple.Create("Naresh", "Suresh", 6), Tuple.Create("Ramesh", "Rupesh", 6), Tuple.Create("xyz", "abc", 10), Tuple.Create("pqr", "asdd", 6), Tuple.Create("asdsd", "rewe", 12), Tuple.Create("wesd", "sfyere", 6), Tuple.Create("sdyed", "hesyd", 12), Tuple.Create("ghedg", "sged", 12) }; var result = data .Where((item, index) => index < data.Count - 1 && item.Item3 == 6 && data[index + 1].Item3 == 12) .FirstOrDefault(); if (result != null) { Console.WriteLine($"{result.Item1}\t{result.Item2}\t{result.Item3}"); } } }