using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Test
{
public static void Main()
{
var customerAddresses = new[]
{
new // address #1
{
customer_id = 6676979,
customer_address_seq = 1,
customer_address_match_codes = new []
{
new
{
customer_address_seq = 1,
customer_id = 6676979,
customer_match_code_id = 5
}
}
},
new // address #2
{
customer_id = 6677070,
customer_address_seq = 1,
customer_address_match_codes = new []
{
new
{
customer_address_seq = 1,
customer_id = 6677070,
customer_match_code_id = 4
},
new
{
customer_address_seq = 1,
customer_id = 6677070,
customer_match_code_id = 5
},
new
{
customer_address_seq = 1,
customer_id = 6677070,
customer_match_code_id = 3
}
}
},
new // address #3
{
customer_id = 6677070,
customer_address_seq = 2,
customer_address_match_codes = new []
{
new
{
customer_address_seq = 2,
customer_id = 6677070,
customer_match_code_id = 4
},
new
{
customer_address_seq = 2,
customer_id = 6677070,
customer_match_code_id = 5
}
}
}
};
var result = (from c in customerAddresses
let isMatch = c.customer_address_match_codes
.Where (cu => cu.customer_match_code_id == 4).Any () &&
c.customer_address_match_codes
.Where (cu => cu.customer_match_code_id == 5).Any ()
let betterMatch = isMatch && c.customer_address_match_codes
.Where (cu => cu.customer_match_code_id == 3).Any () ? 1 : 0
where isMatch == true
orderby betterMatch descending, c.customer_id, c.customer_address_seq
select c)
.FirstOrDefault ();
Console.WriteLine(result);
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLkxpbnE7CnVzaW5nIFN5c3RlbS5UZXh0OwoKcHVibGljIGNsYXNzIFRlc3QKewoJcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQoJeyAKCQkgICAgdmFyIGN1c3RvbWVyQWRkcmVzc2VzID0gbmV3W10KCQkgICAgewoJCSAgICAgIG5ldyAvLyBhZGRyZXNzICMxCgkJICAgICAgewoJCSAgICAgICAgY3VzdG9tZXJfaWQgPSA2Njc2OTc5LAoJCSAgICAgICAgY3VzdG9tZXJfYWRkcmVzc19zZXEgPSAxLAoJCSAgICAgICAgY3VzdG9tZXJfYWRkcmVzc19tYXRjaF9jb2RlcyA9IG5ldyBbXQoJCSAgICAgICAgewoJCSAgICAgICAgICBuZXcKCQkgICAgICAgICAgewoJCSAgICAgICAgICAgIGN1c3RvbWVyX2FkZHJlc3Nfc2VxID0gMSwKCQkgICAgICAgICAgICBjdXN0b21lcl9pZCA9IDY2NzY5NzksCgkJICAgICAgICAgICAgY3VzdG9tZXJfbWF0Y2hfY29kZV9pZCA9IDUKCQkgICAgICAgICAgfQoJCSAgICAgICAgfQoJCSAgICAgIH0sCgkJICAgICAgbmV3IC8vIGFkZHJlc3MgIzIKCQkgICAgICB7CgkJICAgICAgICBjdXN0b21lcl9pZCA9IDY2NzcwNzAsCgkJICAgICAgICBjdXN0b21lcl9hZGRyZXNzX3NlcSA9IDEsCgkJICAgICAgICBjdXN0b21lcl9hZGRyZXNzX21hdGNoX2NvZGVzID0gbmV3IFtdCgkJICAgICAgICB7CgkJICAgICAgICAgIG5ldwoJCSAgICAgICAgICB7CgkJICAgICAgICAgICAgY3VzdG9tZXJfYWRkcmVzc19zZXEgPSAxLAoJCSAgICAgICAgICAgIGN1c3RvbWVyX2lkID0gNjY3NzA3MCwKCQkgICAgICAgICAgICBjdXN0b21lcl9tYXRjaF9jb2RlX2lkID0gNAoJCSAgICAgICAgICB9LAoJCSAgICAgICAgICBuZXcKCQkgICAgICAgICAgewoJCSAgICAgICAgICAgIGN1c3RvbWVyX2FkZHJlc3Nfc2VxID0gMSwKCQkgICAgICAgICAgICBjdXN0b21lcl9pZCA9IDY2NzcwNzAsCgkJICAgICAgICAgICAgY3VzdG9tZXJfbWF0Y2hfY29kZV9pZCA9IDUKCQkgICAgICAgICAgfSwKCQkgICAgICAgICAgbmV3CgkJICAgICAgICAgIHsKCQkgICAgICAgICAgICBjdXN0b21lcl9hZGRyZXNzX3NlcSA9IDEsCgkJICAgICAgICAgICAgY3VzdG9tZXJfaWQgPSA2Njc3MDcwLAoJCSAgICAgICAgICAgIGN1c3RvbWVyX21hdGNoX2NvZGVfaWQgPSAzCgkJICAgICAgICAgIH0KCQkgICAgICAgIH0KCQkgICAgICB9LAoJCSAgICAgIG5ldyAvLyBhZGRyZXNzICMzCgkJICAgICAgewoJCSAgICAgICAgY3VzdG9tZXJfaWQgPSA2Njc3MDcwLAoJCSAgICAgICAgY3VzdG9tZXJfYWRkcmVzc19zZXEgPSAyLAoJCSAgICAgICAgY3VzdG9tZXJfYWRkcmVzc19tYXRjaF9jb2RlcyA9IG5ldyBbXQoJCSAgICAgICAgewoJCSAgICAgICAgICBuZXcKCQkgICAgICAgICAgewoJCSAgICAgICAgICAgIGN1c3RvbWVyX2FkZHJlc3Nfc2VxID0gMiwKCQkgICAgICAgICAgICBjdXN0b21lcl9pZCA9IDY2NzcwNzAsCgkJICAgICAgICAgICAgY3VzdG9tZXJfbWF0Y2hfY29kZV9pZCA9IDQKCQkgICAgICAgICAgfSwKCQkgICAgICAgICAgbmV3CgkJICAgICAgICAgIHsKCQkgICAgICAgICAgICBjdXN0b21lcl9hZGRyZXNzX3NlcSA9IDIsCgkJICAgICAgICAgICAgY3VzdG9tZXJfaWQgPSA2Njc3MDcwLAoJCSAgICAgICAgICAgIGN1c3RvbWVyX21hdGNoX2NvZGVfaWQgPSA1CgkJICAgICAgICAgIH0KCQkgICAgICAgIH0KCQkgICAgICB9CgkJICAgIH07CgkJICAgIAoJCSAgICAgICAgdmFyIHJlc3VsdCA9IChmcm9tIGMgaW4gY3VzdG9tZXJBZGRyZXNzZXMKCQkgICAgICAgICAgICBsZXQgaXNNYXRjaCA9IGMuY3VzdG9tZXJfYWRkcmVzc19tYXRjaF9jb2RlcwoJCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLldoZXJlIChjdSA9PiBjdS5jdXN0b21lcl9tYXRjaF9jb2RlX2lkID09IDQpLkFueSAoKSAmJgoJCSAgICAgICAgICAgICAgICAgICAgICAgICAgYy5jdXN0b21lcl9hZ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