Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
sir

I create two tables in sqlserver Those are Employee,Dept
I contain a common column column in both that is deptno

I have a button control in my application when iam click button these two tables are joined and displayed in gridview using LINQ query


Pls reply me.
Posted
Updated 27-May-11 5:42am
v2

Here is what it looks like.
C#
var query = from e in Employee
join d in Dept on  e.deptno equals d.deptno
select new {e.deptno, d.deptno};


[EDITED]
See the links below, How to map your table to LINQ to Entity framework.
A LINQ Tutorial: Mapping Tables to Objects[^]
ADO Entity framework.[^]
 
Share this answer
 
v3
Comments
kranthi.oru 28-May-11 1:38am    
how can i give directly employee table name and dept table name directly into the linq query.I copied above tables in two datatables
Pls give me correct solutin.
Wonde Tadesse 28-May-11 8:35am    
The name of the table depends on how it's mapped into LINQ to Entity framework.Please look the updated solution.
Manfred Rudolf Bihy 28-May-11 21:11pm    
My 5!
Wonde Tadesse 28-May-11 23:45pm    
Thanks
D-Kishore 3-Sep-12 2:25am    
nice 5:)
Hi Kranthi,

Try in Google with 'Joins in LINQ'. See Google Results[^]

Some useful links here
Link 1[^]
Link 2[^]
LINQ Query Expressions [^]
 
Share this answer
 
Comments
Manfred Rudolf Bihy 28-May-11 21:16pm    
Countered! 5+
D-Kishore 3-Sep-12 2:26am    
nice links 5
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.Reflection;


namespace LinqJOin
{
    public partial class Form1 : Form
    {
       // SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrings["MysqlCon"].ToString());
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
        //    SqlDataAdapter da1 = new SqlDataAdapter("select * from linqemp", Con);
        //DataTable dt1 = new DataTable();
        //da1.Fill(dt1);
        //dataGridView1.DataSource = dt1;



        //SqlDataAdapter da2 = new SqlDataAdapter("select * from emp1 ", Con);
        //DataTable dt2 = new DataTable();
        //da2.Fill(dt2);
        //dataGridView2.DataSource = dt2;
            DataTable dt = new DataTable();
            dt.Columns.Add("ID",typeof(string));
            dt.Columns.Add("Name", typeof(string));

            DataRow dr = dt.NewRow();
            dr["ID"] = "1";
            dr["Name"] = "Test1";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = "2";
            dr["Name"] = "Test2";
            dt.Rows.Add(dr);

            DataTable dt1 = new DataTable();
            dt1.Columns.Add("ID", typeof(string));
            dt1.Columns.Add("Product", typeof(string));

            dr = dt1.NewRow();
            dr["ID"] = "1";
            dr["Product"] = "Test-Product";
            dt1.Rows.Add(dr);
            dataGridView1.DataSource = dt;
            dataGridView2.DataSource = dt1;



            var c = from p in dt.AsEnumerable()
                    join d in dt1.AsEnumerable() on p.Field<string>("ID") equals d.Field<string>("ID")
                    select new { ID = p.Field<string>("ID"), Name = p.Field<string>("Name"), Product = d.Field<string>("Product")};
            DataTable temp = new DataTable();
            temp = ConvertToDataTable(c);
            dataGridView3.DataSource = temp;
            comboBox1.Text = "ID";
            comboBox1.DataSource = temp;
          





           
        }
        #region LinqDatatable
        //For LinQ Application
        public DataTable ConvertToDataTable<T>(IEnumerable<T> varlist)
        {
            DataTable dtReturn = new DataTable();

            // column names   
            PropertyInfo[] oProps = null;

            if (varlist == null) return dtReturn;

            foreach (T rec in varlist)
            {
                // Use reflection to get property names, to create table, Only first time, others will follow   
                if (oProps == null)
                {
                    oProps = ((Type)rec.GetType()).GetProperties();
                    foreach (PropertyInfo pi in oProps)
                    {
                        Type colType = pi.PropertyType;

                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                        {
                            colType = colType.GetGenericArguments()[0];
                        }

                        dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                    }
                }

                DataRow dr = dtReturn.NewRow();

                foreach (PropertyInfo pi in oProps)
                {
                    dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
                    (rec, null);
                }

                dtReturn.Rows.Add(dr);
            }
            return dtReturn;
        }
        #endregion
    }
}
 
Share this answer
 
v2
Google[^] is your friend and 1 link[^] among them.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 28-May-11 21:16pm    
Countered! 5+
you can execute linq query and use the output data to bind in a datagridview to show data to your users:
C#
dataGridView1.AutoGenerateColumns = false;
var BindInformation = from p in tbl_PersonInfo
join f in Financial on p.ID equals f.FinancialPersonID
select new {p.Name,p.Family,f.DebtValue,f.BestankarValue};

 dataGridView1.DataSource = BindInformation;
 
Share this answer
 
v3
var getList = (from t1 in _objTTMSEntities.AssignedTaskInfoes join t2 in _objTTMSEntities.UserMasters
on t1.AssignedBy equals t2.UserID
where t1.AssignedToRoleType =="D"
select new { t1.ProjectName, t1.TaskID, t1.TaskName, t1.AssignedTo, t2.UserName,t1.StartDt, t1.EndDt }).ToList();



Joining three tables

SQL
var getList = (from t1 in _objTTMSEntities.AssignedTaskInfoes
                           join t2 in _objTTMSEntities.UserMasters on t1.AssignedBy equals t2.UserID
                           join t3 in _objTTMSEntities.UserMasters on t1.AssignedTo equals t3.UserID
                               where t1.AssignedToRoleType =="D"
                           select new { t1.ProjectName, t1.TaskID, t1.TaskName, t1.AssignedTo, t2.UserName,t1.StartDt, t1.EndDt }).ToList();

Note:select the columns from t3 table as required.
 
Share this answer
 
v2
Comments
CHill60 3-Sep-15 8:12am    
Question is over 4 years old and already resolved.

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