1

I have two tables TABLE_A and TABLE_B which are related.

I need to convert this SQL Server query to a LINQ lambda query in C#:

SELECT a.COL1, a.COL2, b.COL2
FROM TABLE_A a, TABLE_B b
WHERE b.COL1 = A.COL3

Actually b.col1 is pk in table_b and a.col3 is fk of that in table_a

Rouzbeh Zarandi
  • 1,047
  • 2
  • 16
  • 34
  • 3
    Generally, a "please write my code for me" type of question is considered off-topic here. You should include your attempt(s) so users can help you improve on them. – Jacob H Apr 30 '18 at 17:01
  • Probably, these might help you https://stackoverflow.com/questions/17638625/convert-sql-query-into-lambda-expression https://stackoverflow.com/questions/24197602/convert-sql-query-to-lambda-expression https://stackoverflow.com/questions/3854146/how-to-convert-this-sql-query-to-linq-or-lambda-expression – mallela prakash Apr 30 '18 at 17:01
  • @JacobH thank you for tip and respond ... but unfortunately in this case i have no idea how to start ..... im new in linq query ..i have done some things but not different cols from different tables – Rouzbeh Zarandi Apr 30 '18 at 17:28
  • 2
    Perhaps start by reading my [SQL to LINQ Recipe](https://stackoverflow.com/questions/49245160/sql-to-linq-with-multiple-join-count-and-left-join/49245786#49245786) ? – NetMage Apr 30 '18 at 17:44
  • @NetMage nice post – Jacob H Apr 30 '18 at 18:40

2 Answers2

1

Navigation properties are powerful:

table_a.Select( a=> new {a.COL1 , a.COL2 , a.b.COL2} )

Also, you can learn how to write better questions at "How to create a Minimal, Complete, and Verifiable example" I suggest to you to read post carefully.

dani herrera
  • 48,760
  • 8
  • 117
  • 177
0

var q = (from a in table_A join b in table_B on b.col1 equals a.col3 select new {a.COL1, a.COL2, b.COL2}).ToList();