aka baka
aka baka

Reputation: 223

How to JOIN two data tables using LINQ and Where condition?

I have two data tables as following

    TableA      TableB
    UID         QID
    QID         Value1
                Value2

I want to join two data tables and get Value1 and Value 2. I tried the following, but I am not getting any data back.

string str = "AAA";
var result = (from t1 in TableA.AsEnumerable()
    //join between two tables
    join t2 in TableB.AsEnumerable() on t1.Field<string>("UID") equals t2.Field<string>("QID")
    //where conditions
    where t2.Field<string>("QID") == str && t1.Field<string>("QID") == "ABCD"
    //select clause
    select t2.Field<string>("Value1"));

This is my query with out using LINQ and its working.

  Select t2.Value1, t2.value2 from  TableA t1 JOIN  TableB t2  ON t2.QID = t1.UID WHERE t2.QID = 'AAAAA' AND t1.QID = 'XXXX'

Thanks for your help.

Upvotes: 1

Views: 334

Answers (1)

Selim Yildiz
Selim Yildiz

Reputation: 5370

Your LINQ code should work, I guess you just need to use the same where the condition values for QID.

 where t2.Field<string>("QID") == "AAA" && t1.Field<string>("QID") == "ABCD"

is not equal to your SQL:

 WHERE t2.QID = 'AAAAA' AND t1.QID = 'XXXX'

It should work when you set correct values in LINQ

Upvotes: 1

Related Questions