25304665 Linq with DefaultIfEmty with select new {}

CSharp · Console

/Program.cs

using System;
using System.Linq;

public class Program
{
	public class PersonPOCO
	{
		public string name;
		public string country;
	/* and some more properties in your db */
	}

	public static void Main()
	{
		var persons = new[]
		{
			new PersonPOCO
			{
				name = "test",
				country = "FOO"
			},
			new PersonPOCO
			{
				name = "bar",
				country = "FOO"
			}
		};
		var oPerson = new {
			name = "test1",
			country = string.Empty
		};
		var name_country = (from person in persons
							where person.name == oPerson.name || person.country == oPerson.country
							select new {
								person.name,
								person.country
							}).DefaultIfEmpty(new {
			oPerson.name,
			oPerson.country
		}).First();
		Console.WriteLine("name: {0}; country: {1}", name_country.name, name_country.country);
	}
}
;