.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Code Snippets >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

LINQ, where in (Value1, Value2, Value3.) equivalent Statement.

Posted By:Sajid P K       Posted Date: November 09, 2010    Points: 15    Category: LINQ    URL: http://www.dotnetspark.com  

This Code Segment Help you to write Where in (x1, x2 ) equivalent statement in LINQ. The "IN" clause is built in linq via the .Contains() method

While Working, I have come across scenario to write Linq equivalent for following query.


SELECT     ID,Name, ContactTitle, Address,   City, Region, PostalCode, Country, Phone
FROM    Author WHERE    Country IN ('UK', 'USA', 'Australia')

Please see the Equivalent statement in LINQ below...

Query syntax 


string[] countries = new string[] { "UK", "USA", "Australia" };

var Author =    from c in context.Author s   
                   where countries.Contains(c.Country)   
                   select c;


Lambda expression

string[] countries = new string[] { "UK", "USA", "Australia" };

var Author =    Author.Where(c => countries.Contains(c.Country));


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend