MD GHOUSE MD GHOUSE - 4 months ago 19
SQL Question

how to convert this query into linq

Please help me how to convert sql query into linq & also if in future i have any query to change is their any online solution is their for converting sql query into linq

select
e.countryId,
s.CountryName
from
emp e Inner join
Country s on e.countryId=s.CountryId

Answer

Use the following:

var result =  from t1 in db.Table1 
              join t2 in db.Table2 on t1.field equals t2.field 
              select new { t1.field2, t2.field3 };

The above join two tables and returns required data.

So your query would be like:

   var result =  from e in db.emp 
                 join s in db.Country on e.countryId equals s.CountryId 
                 select new { e.countryId, s.CountryName };

I would suggest to learn the basics of Linq and use this link to study:

https://msdn.microsoft.com/en-us/library/bb397927.aspx

By the way, you can use LinqPad to practice Linq and it is a good tool.