top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to Use LINQ query on a DataTable in C#?

0 votes
356 views
How to Use LINQ query on a DataTable in C#?
posted Apr 20, 2016 by Sathyasree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

namespace AbundantCode

{

internal class Program

{

//How to Use LINQ query on a DataTable in C# ?

private static void Main(string[] args)

{

List<CodeSnippet> CodeLst = new List<CodeSnippet>();

DataTable dt = new DataTable();

dt.Columns.Add("Name");

dt.Columns.Add("Code");

DataRow row1 = dt.NewRow();

row1["Name"] = "Abundantcode";

row1["Code"] = "1-1-1";

dt.Rows.Add(row1);

DataRow row2 = dt.NewRow();

row2["Name"] = "Plenty of sourcecode";

row2["Code"] = "1-1-12";

dt.Rows.Add(row2);

var results = (from m in dt.AsEnumerable()

where m.Field<string>("Code") == "1-1-12"

select m).FirstOrDefault();

Console.WriteLine(results["Name"]);

Console.ReadLine();

}

}

public class CodeSnippet

{

public string Name { get; set; }

public string Code { get; set; }

}

}
answer Apr 20, 2016 by Shivaranjini
...