Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 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 6:35am
Edited 27-May-11 6: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 at 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 at 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 at 28-May-11 21:11pm
   
My 5!
Wonde Tadesse at 28-May-11 23:45pm
   
Thanks
kishore doni at 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 at 28-May-11 21:16pm
   
Countered! 5+
kishore doni at 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 at 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

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

  Print Answers RSS
0 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 152
2 ProgramFOX 130
3 Maciej Los 130
4 den2k88 80
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 3 Sep 2012
Copyright © CodeProject, 1999-2014
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