Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have three data table (t1,t2,t3) in frontened i want to join this tables to make a single table how to do this using linq query ....


I am using this but it doesn't work and also i want to show output in datatable form to perform other action

C#
var query = from a in t1
            join b in t2 on a.someFieldID equals b.someFieldID
             join c in t3 on b.someFieldID equals c.someFieldID
            select a;



Thanks and regards
Srishti
Posted

C#
DataTable dtResult = new DataTable();
dtResult.Columns.Add("ID", typeof(string));
dtResult.Columns.Add("name", typeof(string));
dtResult.Columns.Add("stock", typeof(int));

var result = from dataRows1 in table1.AsEnumerable()
             join dataRows2 in table2.AsEnumerable()
             on dataRows1.Field<string>("ID") equals dataRows2.Field<string>("ID")

             select dtResult.LoadDataRow(new object[]
             {
                dataRows1.Field<string>("ID"),
                dataRows1.Field<string>("name"),
                dataRows2.Field<int>("stock"),
              }, false);
result.CopyToDataTable();

Code originally written by Sven Grosen: http://stackoverflow.com/a/20760794[^]
 
Share this answer
 
v3
Comments
srishti_ 1-Sep-14 2:58am    
please write it properly in readable format...
var result = from a in dtdrug.AsEnumerable()
join b in dtcui.AsEnumerable()
on a.Field<string>("") equals b.Field<string>("")
join c in dtrx.AsEnumerable()
on b.Field<string>("") equals c.Field<string>("")

I understand this but it showing error in second table join
Thomas Daniels 1-Sep-14 5:44am    
Please provide attribution when you use code from an external source in your answer.
if you are using datatable for that you have to use AsEnumerable() method for linq
And your query should be like below you can change your FKey relationship
var query = (from a in t1.AsEnumerable()
                     join b in t2.AsEnumerable() on a.Field<int>("id") equals b.Field<int>("fkey")
                     join c in t3.AsEnumerable() on b.Field<int>("id") equals c.Field<int>("fkey")
                     select a).ToList();
        //If You are selecting a single table then you can convert to table on the following way
        if (query.Count > 0)
        {
            DataTable newDt = query.CopyToDataTable();
        }

Get More info HERE[^]
 
Share this answer
 
v2
Comments
srishti_ 1-Sep-14 3:01am    
thanks but how to convert in into datatable
[no name] 1-Sep-14 3:11am    
welcome
see again updated solution not exact but the idea is like this
harshavardhan12345678 1-Sep-14 3:17am    
you can use this extension method and call it like this.

public static DataTable ToDataTable<t>(List<t> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);

//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900