Click here to Skip to main content
12,403,812 members (75,606 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C# ASP.NET LINQ
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 27-May-11 5:35am
Updated 27-May-11 5:42am
TweakBird17.1K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Here is what it looks like.
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.[^]
  Permalink  
v3
Comments
kranthi from madras 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 R. Bihy 28-May-11 21:11pm
   
My 5!
Wonde Tadesse 28-May-11 23:45pm
   
Thanks
kishore doni 3-Sep-12 2:25am
   
nice 5:)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi Kranthi,

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

Some useful links here
Link 1[^]
Link 2[^]
LINQ Query Expressions [^]
  Permalink  
Comments
Manfred R. Bihy 28-May-11 21:16pm
   
Countered! 5+
kishore doni 3-Sep-12 2:26am
   
nice links 5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Google[^] is your friend and 1 link[^] among them.
  Permalink  
Comments
Manfred R. Bihy 28-May-11 21:16pm
   
Countered! 5+
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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
    }
}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

you can execute linq query and use the output data to bind in a datagridview to show data to your users:
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;
  Permalink  
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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

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.
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 3 Sep 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100