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

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Forum >> LINQ >> Post New QuestionBookmark and Share Subscribe to Forum

Write a LINQ query for a dataset?

Posted By: Ramesh     Posted Date: September 01, 2010    Points:2   Category :LINQ

How can you write a LINQ query to a dataset object. Please explain this with one example i am using C# as code language.


Author: Sunil Yadav       [Moderator]      
Posted Date: September 01, 2010     Points: 5   


Try something like this.

 DataSet ds = new DataSet();
var query = from p in ds.Tables[0].AsEnumerable()
where p.Table.Columns["Name"].ToString() == "sunil"
select p;

Author: Asha.MP             
Posted Date: September 06, 2010     Points: 5   


This is in vb, you can use this logic in C#, you will get the required data

Option Strict On
Option Explicit On

Public Class Test
Shared Sub main()
'Data set to store the value
Dim dset As New DataSet()

'Data table
Dim dt As New DataTable

'Adding columns to data table
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Address", GetType(String))

'Adding rows to the data table
dt.Rows.Add("Asha", "1", "Tumkur")
dt.Rows.Add("Usha", "2", "Bangalore")
dt.Rows.Add("Ashok", "3", "")
dt.Rows.Add("", "4", "Mysore")
dt.Rows.Add("", "5", "")

'Adding data table to the Data set

'LINQ to get required data
Dim DataWithName = From detail In dset.Tables(0) Where detail.Item("Name") IsNot String.Empty Select detail
Dim DataWithOutName = From detail In dset.Tables(0) Where detail.Item("Name") Is String.Empty Select detail

Dim DataWithAddress = From detail In dset.Tables(0) Where detail.Item("Address") IsNot String.Empty Select detail
Dim DataWithotAddress = From detail In dset.Tables(0) Where detail.Item("Address") Is String.Empty Select detail

'Displying the result
For Each rows As DataRow In DataWithName

End Sub

End Class


Thanks and Regards,

Thanks and Regards,
Author: Sethuraman             
Posted Date: February 15, 2011     Points: 5   

Hi friend

I have tried but i couldn't get it need some explicit conversion to change linq query result to data set with this i will give query how to select using LINQ and fill the result in grid view .

protected void Button1_Click(object sender, EventArgs e)
DataSet ds = new DataSet();
var result = from sel in db.Emp_personel select sel;
// ds = result
GridView1.DataSource = result;

design page here i have used master page

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
Welcome to ASP.NET!
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:GridView ID="GridView1" runat="server"
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.

i think this may help you

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

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