.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Manipulate DataTable with LinQ

Posted By:      Posted Date: October 13, 2010    Points: 0   Category :.NET Framework

I Have to manipulate filtered rows of DataTable.

Should I use the extension function dataTable.AsEnumarable() in order to filter by the extension function Where(), and the loop by foreach on the filtered IEnumerable<DataRow> collection,


should I loop by foreach and ask if() on each row?

What is better?

View Complete Post

More Related Resource Links

Linq query to DataTable


Is there option to convert from Linq.IQueryable to DataTable with some sort of CopyToDataTable() function. I am trying this.
     var row = from p in dc.ProductModels
                  select p;
    DataTable dt = row.CopyToDataTable();

Is there option to convert from Linq.IQueryable to DataTable with some sort of CopyToDataTable() function. I am trying this.

     var row = from p in dc.ProductModels


How to handle null while converting from my datatable to List<> using LINQ


I am converting my datatable to LISt using LINQ
how do I handle nulls coming from database

List<Port> portDetails = new List<Port>();
DataTable dt = ds.Tables[0];               
portDetails = (from q in dt.AsEnumerable()
select new Port
PortCode = q.Field<string>("Code"),
ExtCode = q.Field<string>("Nb"),
Name = q.Field<string>("Name"))

In the above query if Code is null I do not want property
portcode to be set to the value it should only set if it is
not null or not blank 
PortCode = q.Field<string>("Code"),

What should be syntax
I was trying somethign like this which doesnt work
Portcode = q.Field<bool>("Code") == null ? null : q.Field<bool>("Code")


delete a row in datatable using linq to dataset

hihow can i delete a row in datatable using linq to dataset?I have a dataset in my project, need to delete some rows filtered by IDhow can i do?

Simple question.... LINQ on Datatable

Hi all,Simple one this I am sure for those who use LINQ! :)I basically have two tables that will be stored in viewstate until I save the data to the database.  I need a way to query the datatables, delete remove etc.  Is this possible in LINQ?So for example, delete/select rows from the DataTable that have a particular ID etc...Thanks,Mark

Linq to SQL insert to datatable


I got the following query to group the table. I am wondering how to add a subtotal record with anonymous type.

string prevOriginator = string.Empty;
var oOriginator = (from I in dtIssue.AsEnumerable()
                            group I by new {RaisedBy = I.Field<string>("RaisedBy"), ItemType = I.Field<string>("ItemType")} into grp
                            orderby grp.Key
                            select new {RecType = "D",  RaisedBy = grp.Key.RaisedBy, ItemType = grp.Key.ItemType, Count = grp.Count() });
foreach (var record in oOriginator)
    if (record.RaisedBy != prevOriginator)
        if (prevOriginator != string.Empty)
             //add a subtotal record

Better performance for manipulate DataTable



I want to know if it is recommended to use LINQ to objects for do something for each row in a DataTable, or it gets me bad performance comparison to do simple for-each loop  on DataTable's rows. I love use LINQ because it is nicer code, but I don't want get bad performance.

In simple words, what is the more recommended code:

1. Use simple ForEach loop



(DataRow row in myDataTable.Rows)

row[1] =


2. Use LINQ foreach

myDataTable.AsEnumerable().ToList().ForEach(row => row[1] =

How to compare two different structure datatable by using LINQ



   i have following senario in my application

DataTable1 have following structure

Dependencies                                      Tolerance

Workflow1 Lorem1 lpsum           150     

Workflow2 Lorem1 lpsum            25


DataTable 2


DataTable Comparition by usin LINQ



    i have following senario in my application

DataTable1 have following structure


Dependencies                              Tolerance

Workflow1 Lorem1 lpsum                      150  
Workflow2 Lorem1 lpsum                       25
DataTable 2

Dep_Type         Check      Dependencies

"Star Workflow" "false" "Workflow1 Lorem1 lpsum"
"Star Workflow" "false" "Workflow2 Lorem1 lpsum"
Star Workflow" "false" "Workflow4 Lorem lpsum"
Star Workflow" "false" "Workflow5 Lorem lpsum"


I want to compare two datatable, here i want to display different records from DataTable 2. how i can achieve it.


My Output should come like that, how i can achieve it through LINQ

Moving data from datatable to XML using Linq


I have a data table I need to convert to XML. I know I can use datable.WriteXML but I need to rename the elements as the column names coming from the Oracle db are ugly. I found this snippet somewhere:

XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
                        new XElement("products", from p in dt
                         select new XElement("product",
                             new XAttribute("guid", p.ProductId),
                             new XAttribute("title", p.Title),
                             new XAttribute("version", p.Version))));


It looks like

Playing with Linq grouping: GroupByMany?

One of its features is grouping. Many people understand grouping like it is defined in Sql. Linq is implementing grouping quite the same way. Let's discover this syntax and how to make consecutive groups easier.

LINQ : Implementing IN and NOT IN

I got tried of typing

var result = from s in source
where items.Contains(s)
select s;and so I implemented the IN and NOT IN methods as extension methods:

101 LINQ Samples Tutorials

101 LINQ Example with sample code snippets....This sample prints each element of an input integer array whose value is less than 5. The sample uses a query expression to create a new sequence of integers and then iterates over each element in the sequence, printing its value

Linq: how to share parameters between lambda expressions ?

When using Linq to objects, you will quickly feel the need to pass some parameters from a method to another but it's not so easy because each Linq method is not calling the following one. In a Linq sequence, each method is using the result computed by the previous one. So, local contexts are not visible from one method to another.
The compiler is using two technical different ways to let parameters go out of a method.

Linq to Sql CompiledQuery container

Ok now let's go. Here is just a little trick but with some interesting patterns that could be useful in some other contexts not connected to Linq to Sql.

When using Linq expressions, like with Linq to Sql, translating the expression into something else (sql for example) is taking time and resources. Sometimes it's negligible, sometimes not...

LINQ to SQL Paging using GridView in C# and ASP.NET 3.5

This tutorial will show you how we can extend the LINQ to SQL Class and create methods that will allow us to page data from a SQL database. C# version.

What is Lambda Expressions In LINQ

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x." This expression can be assigned to a delegate type as follows:

How to create 3 tier application using LINQ

As you know that in 3 tier architecture there are three layers

User interface layer. (Is our Form in Windows application and .aspx page in Web application)
Data Access layer. (Which provides interface between Business logic layer and Database)
Business Logic layer.(Which stores your application logic)
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend