Tuesday, 3 December 2013

join two sharepoint list using c#

Share it Please

1.Create a custom list name as "List1" with below column

2.Create a custom list name as "List2" with below column

3.paste the below code inside the webpart

public void GetEmployee()
        {
            SPWeb objweb = SPContext.Current.Web;
            SPList olist1 = objweb.Lists["list1"];
            SPListItemCollection collListItems = olist1.Items;
            SPQuery oQuery1 = new SPQuery();
            DataTable table1 = collListItems.GetDataTable();
            SPList olist2 = objweb.Lists["list2"];
            SPListItemCollection collListItems2 = olist2.Items;
            DataTable table2 = collListItems2.GetDataTable();
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Age", typeof(string));
            dt.Columns.Add("Salary", typeof(string));
            dt.Columns.Add("Sex", typeof(string));
            dt.Columns.Add("Address", typeof(string));
            var Vout = from tbl1 in table1.AsEnumerable()
                       join tbl2 in table2.AsEnumerable() on tbl1["Name"] equals tbl2["Name"]
                       select new
                       {
                           Sex = tbl2["Sex"],
                           Address = tbl2["Address"],
                           Name = tbl1["Name"],
                           Age = tbl1["Age"],
                           Salary = tbl1["Salary"]
                       };
            foreach (var item in Vout)
            {
                DataRow dr = dt.NewRow();
                dr["Name"] = item.Name;
                dr["Age"] = item.Age;
                dr["Salary"] = item.Salary;
                dr["Sex"] = item.Sex;
                dr["Address"] = item.Address;
                dt.Rows.Add(dr);
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
4.output



1 comment:

  1. var Vout = from tbl1 in table1.AsEnumerable()
    join tbl2 in table2.AsEnumerable() on tbl1["Name"] equals tbl2["Name"]
    select new
    {
    Sex = tbl2["Sex"],
    Address = tbl2["Address"],
    Name = tbl1["Name"],
    Age = tbl1["Age"],
    Salary = tbl1["Salary"]
    };

    These lines i cant execute..

    ReplyDelete

Followers

Follow The Author