fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. var customerAddresses = new[]
  11. {
  12. new // address #1
  13. {
  14. customer_id = 6676979,
  15. customer_address_seq = 1,
  16. customer_address_match_codes = new []
  17. {
  18. new
  19. {
  20. customer_address_seq = 1,
  21. customer_id = 6676979,
  22. customer_match_code_id = 5
  23. }
  24. }
  25. },
  26. new // address #2
  27. {
  28. customer_id = 6677070,
  29. customer_address_seq = 1,
  30. customer_address_match_codes = new []
  31. {
  32. new
  33. {
  34. customer_address_seq = 1,
  35. customer_id = 6677070,
  36. customer_match_code_id = 4
  37. },
  38. new
  39. {
  40. customer_address_seq = 1,
  41. customer_id = 6677070,
  42. customer_match_code_id = 5
  43. },
  44. new
  45. {
  46. customer_address_seq = 1,
  47. customer_id = 6677070,
  48. customer_match_code_id = 3
  49. }
  50. }
  51. },
  52. new // address #3
  53. {
  54. customer_id = 6677070,
  55. customer_address_seq = 2,
  56. customer_address_match_codes = new []
  57. {
  58. new
  59. {
  60. customer_address_seq = 2,
  61. customer_id = 6677070,
  62. customer_match_code_id = 4
  63. },
  64. new
  65. {
  66. customer_address_seq = 2,
  67. customer_id = 6677070,
  68. customer_match_code_id = 5
  69. }
  70. }
  71. }
  72. };
  73.  
  74. var result = (from c in customerAddresses
  75. let isMatch = c.customer_address_match_codes
  76. .Where (cu => cu.customer_match_code_id == 4).Any () &&
  77. c.customer_address_match_codes
  78. .Where (cu => cu.customer_match_code_id == 5).Any ()
  79. let betterMatch = isMatch && c.customer_address_match_codes
  80. .Where (cu => cu.customer_match_code_id == 3).Any () ? 1 : 0
  81. where isMatch == true
  82. orderby betterMatch descending, c.customer_id, c.customer_address_seq
  83. select c)
  84. .FirstOrDefault ();
  85.  
  86. Console.WriteLine(result);
  87.  
  88. }
  89. }
Success #stdin #stdout 0.05s 37296KB
stdin
Standard input is empty
stdout
{ customer_id = 6677070, customer_address_seq = 1, customer_address_match_codes = <>__AnonType0`3[System.Int32,System.Int32,System.Int32][] }