LINQ QUERY FOR DATASET IN C#?

Tanuj Kumar

Total Post:134

Points:940
Posted by  Tanuj Kumar
C# 
C#
 1885  View(s)
Ratings:
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 ";

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Linq Query for dataset in c#?

    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

      Modified On Apr-04-2018 04:10:36 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!