Tuesday 8 May 2012

Linq To DataSet


Join Between two Tables

DataTable DT1 = FillDatset("select * from country_Master");
DataTable DT2 = FillDatset("select * from State_Master");

          var List = (from DtCountry in DT1.AsEnumerable()
                           join DtState in DT2.AsEnumerable()
                           on DtCountry.Field<int>("CountryID")
                           equals DtState.Field<int>("CountryID") into DTALL
                           from _dtAll in DTALL.DefaultIfEmpty()
                           select new
                            {
                               CountryID = DtCountry.Field<int>("CountryID"),
                               StateName = (_dtAll != null) ?
                                                   _dtAll.Field<string>("StateName") :
                                                     string.Empty
                             }
                           ).ToList();

          var List1 = (from DtCountry in DT1.AsEnumerable()
                            join DtState in DT2.AsEnumerable()
                            on DtCountry.Field<int>("CountryID")
                            equals DtState.Field<int>("CountryID") into DTALL
                            from _dtAll in DTALL.DefaultIfEmpty()
                            select _dtAll).ToList();

No comments:

Post a Comment