Home > DeveloperSection > Forums > Linq Query for dataset in c#?
Tanuj Kumar

Total Post:134

Points:940
Posted on    February-04-2014 5:43 AM

 C# C# 
Ratings:


 1 Reply(s)
 1207  View(s)
Rate this:

I have dataset which contains two table as

DataTable dtFields = new DataTable("tmpFieldTable");

            dtFields.Columns.Add("FieldID");

            dtFields.Columns.Add("CDGroupID");

            dtFields.Columns.Add("CDCaption");

            dtFields.Columns.Add("fldIndex");

DataTable dtCDGroup = new DataTable("tmpCDGroup");

            dtCDGroup.Columns.Add("CDGroupID");

            dtCDGroup.Columns.Add("Name");

            dtCDGroup.Columns.Add("Priority");

DataSet ds = new DataSet("tmpFieldSet");

            ds.Tables.Add(dtFields);

            ds.Tables.Add(dtCDGroup);

How can I write following SQL quey to LINQ

queryString = "Select FieldID, tmpCDGroup.Name, CDCaption, IIF(ISNULL(Priority),99,Priority), fldIndex from tmpFieldList LEFT OUTER JOIN tmpCDGroup ON tmpFieldList.CDGroupID = tmpCDGroup.CDGroupID order by 4,5 ";



Pravesh Singh

Total Post:411

Points:2881
Posted on    February-04-2014 5:59 AM

Hi Tanuj,

var resultArray = tmpFieldList.Join(

    tmpCDGroup,                           // inner join collection

    fieldList => fieldList.CDGroupID,     // outer key selector

    cd => cd.CDGroupID,                   // inner key selector

    (fieldList, cd) => new {             // result selector

        FieldID = fieldList.FieldID,

        Name = cd.Name,

        CDCaption = cd.CDCaption,

        Priority = fieldList.Priority ?? 99,

        fldIndex = fieldList.fldIndex

     })

.OrderBy(result => result.Priority)

.ThenBy(result => result.fldIndex)

.ToArray();

Then you can access using, for example,

resultArray[0].FieldID


Don't want to miss updates? Please click the below button!

Follow MindStick