fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var fooList = new List<Foo>()
  10. {
  11. new Foo(){First = "a1", Second="b1", Third="c1", Fourth="v1"},
  12. new Foo(){First = "a1", Second="b1", Third="c1", Fourth="v2"},
  13. new Foo(){First = "a1", Second="b1", Third="c2", Fourth="v4"},
  14. new Foo(){First = "a1", Second="b1", Third="c3", Fourth="v3"},
  15. new Foo(){First = "a1", Second="b1", Third="c3", Fourth="v4"}
  16. };
  17.  
  18. var searchText = "v4";
  19.  
  20. var filteredFooList = fooList.GroupBy(foo => foo.Third)
  21. .Where(groupedFoo => groupedFoo.Any(foo => foo.Fourth == searchText))
  22. .SelectMany(groupedFoo => groupedFoo).ToList();
  23.  
  24. foreach (var item in filteredFooList)
  25. {
  26. Console.WriteLine($"{item.First} {item.Second} {item.Third} {item.Fourth}");
  27. }
  28. }
  29.  
  30. public class Foo
  31. {
  32. public string First { get; set; }
  33. public string Second { get; set; }
  34. public string Third { get; set; }
  35. public string Fourth { get; set; }
  36. }
  37. }
Success #stdin #stdout 0.01s 29928KB
stdin
Standard input is empty
stdout
a1 b1 c2 v4
a1 b1 c3 v3
a1 b1 c3 v4