Используйте Select
, чтобы спроецировать «оценку соответствия» с клиентом, а затем заказать:
class Program
{
static void Main(string[] args)
{
var Customers = new Customer[]
{
new Customer { FirstName = "Foo", LastName = "Laurie" },
new Customer { FirstName = "Bar", LastName = "Jackson" },
new Customer { FirstName = "Jackson", LastName = "Bro" },
new Customer { FirstName = "Laurie", LastName = "Foo" },
new Customer { FirstName = "Jackson", LastName = "Laurie" },
};
string[] searchTerms = new string[] { "Jackson", "Laurie" };
//want to find those customers with first, last or BOTH names in the searchTerms
var matchingCusts = Customers
.Where(m => searchTerms.Contains(m.FirstName)
|| searchTerms.Contains(m.LastName))
.ToList();
var result = matchingCusts.Select(x => new
{
Customer = x,
MatchEvaluation = (searchTerms.Contains(x.FirstName) ? 1 : 0) + (searchTerms.Contains(x.LastName) ? 1 : 0),
})
.OrderByDescending(x => x.MatchEvaluation)
.Select(x => x.Customer);
foreach (var c in result)
{
Console.WriteLine(c.FirstName + " " + c.LastName);
}
Console.ReadKey();
}
public sealed class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}