LINQ Query Syntax

Define Two Classes as Person & Alternate Person

class person

{

public string FirstName { get; set;}

public string LastName { get; set; }

public int Age { get; set;}

}

class alternateperson

{

public string FullName { get; set; }

public int Age { get; set; }

}

class Program

{

static void Main(string[] args)

{

List<person> people = new List<person> { new person{FirstName = "Venu", LastName = "Pavuluri", Age = 28},

new person{FirstName = "Babu", LastName = "Pav", Age = 30},

new person{FirstName = "Victor", LastName = "Serbin", Age = 35}

};

IEnumerable<alternateperson> results = from p in people

select new alternateperson {FullName = p.FirstName + " " + p.LastName, Age = p.Age};

/*people.Select(p => new alternateperson

{

FullName = p.FirstName + " " + p.LastName,

Age = p.Age

});

*/

foreach(var r in results)

Console.WriteLine("Full Name: {0}, \nAge: {1}\n",r.FullName, r.Age);

}

}


Above Linq Expression: from p in people
select new alternateperson {FullName = p.FirstName + " " + p.LastName, Age = p.Age};



Equals to: people.Select(p => new alternateperson

{

FullName = p.FirstName + " " + p.LastName,

Age = p.Age

});



Comments

Popular posts from this blog

Windows Azure Package Build Error: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

Resource ID : 1. The request limit for the database is 180 and has been reached.

How to get Client's Location using IPAddress