Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Return DataSet From DataSet
Message
From
17/07/2010 06:41:59
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
General information
Forum:
ASP.NET
Category:
LINQ
Miscellaneous
Thread ID:
01472665
Message ID:
01472781
Views:
45
>>>>>How do you return a dataset, using Linq, of rows from another dataset?
>>>>
>>>>Here is a sample:
>>>>
>>>>    DataTable tbl = new DataTable();
>>>>	using (OleDbConnection con = new OleDbConnection(
>>>>	"Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;"+
>>>>	@"Data Source=C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Samples\northwind.sdf;"))
>>>>	{
>>>>	  con.Open();
>>>>	  OleDbCommand cmd = con.CreateCommand();
>>>>	  cmd.CommandText = "select * from customers";
>>>>	  OleDbDataReader rdr = cmd.ExecuteReader();
>>>>	  tbl.Load(rdr);
>>>>	  con.Close();
>>>>	}
>>>>	
>>>>	DataTable usaCustomers =
>>>>	 tbl.AsEnumerable()
>>>>	    .Where(c => c.Field<string>("Country") == "USA")
>>>>		.CopyToDataTable();
>>>>	
>>>>	DataTable ukCustomers =
>>>>	 tbl.AsEnumerable()
>>>>	    .Where(c => c.Field<string>("Country") == "UK")
>>>>		.CopyToDataTable();
>>>>	
>>>>//	usaCustomers.Dump("USA");
>>>>//	ukCustomers.Dump("UK");
>>>>
Cetin
>>>
>>>Not so easy with a *DataSet* though :-}
>>
>>Sorry I don't understand what you mean Viv. What is hard in that sample?
>
>Your example is great but it's one Table. Kevin wanted to do that with a DataSet. So how would you know how many tables, what the filter condition might be, what relationships might exist between the tables, etc.etc.
>
> (I wouldn't use a dataset when there is Linq but still I don't think it is very hard).
>
>I don't think you *could* realistically do this with a DataSet?

Well I thought anyone who is using a Dataset would know how to refer to a table in a dataset and create a new dataset and add a new table to it with ease:) Here is a new version ready to run in LinqPad. It is not very different and easy IMHO:
void Main()
{
		string cstr = "Provider=SQLNCLI;"+
		@"server=.\sqlexpress;trusted_connection=yes;database=northwind;";
		
		DataSet ds1 = new DataSet();
		OleDbDataAdapter da = new OleDbDataAdapter("select * from customers", cstr	);
		da.Fill(ds1,"Customers");
		
		DataSet ds2 = new DataSet();

		ds2.Tables.Add(
		ds1.Tables["Customers"].AsEnumerable()
		    .Where(c => c.Field<string>("Country") == "USA")
			.CopyToDataTable());
		ds2.Tables[0].TableName="usa";
		
		ds2.Tables.Add(
		 ds1.Tables["Customers"].AsEnumerable()
		    .Where(c => c.Field<string>("Country") == "UK")
			.CopyToDataTable());
                 ds2.Tables[1].TableName = "uk";

	 ds2.Tables["usa"].Dump("USA");
	 ds2.Tables["uk"].Dump("UK");
}
PS: CopyToDataTable() have overloads to load an existing DataTable but still I wouldn't use a DataSet unless if that is my only chance - for example using VFP as data backend I don't have an EF provider and have to resort using OleDb and datatables or Xml:)
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform