473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

LINQ syntax question

Someone asked me how I would do the following....suppose you have two
ADO.NET datatables, and you want to do the equivalent of SELECT
Table1.*, Table2.* (in other words, do a linq query that joins the
two, and brings in all columns).

For instance...

DataTable dtEmps = new DataTable();
dtEmps.Columns.Add("EmployeeID", typeof(Int32));
dtEmps.Columns.Add("Salary",typeof(Decimal));

dtEmps.Rows.Add(1,90000);
dtEmps.Rows.Add(2,80000);
dtEmps.Rows.Add(3,95000);

DataTable dtNotes = new DataTable();
dtNotes.Columns.Add("EmployeeID", typeof(Int32));
dtNotes.Columns.Add("Notes", typeof(String));
dtNotes.Rows.Add(1, "Notes 1 for Kevin S. Goff");
dtNotes.Rows.Add(1, "Notes 2 for Kevin S. Goff");
dtNotes.Rows.Add(2, "Notes 1 for Steve Goff");
dtNotes.Rows.Add(3, "Notes 1 for Gwen Goff");

var Results = from rowEmpData in dtEmps.AsEnumerable()
join rowNoteData in dtNotes.AsEnumerable()

on rowEmpData.Field<Int32>("EmployeeID") equals
rowNoteData.Field<Int32>("EmployeeID")
where rowEmpData.Field<decimal>("Salary") 90000
select new (rowEmpData.AllColumns, rowNoteData.AllColumns);
My only answer is just to list all the columns in the select...select
new { RowEmpData.Field<Int32>("EmployeeID"), etc.)

Is there a shortcut to pull all columns from both data tables?

Thanks,
Kevin


Dec 9 '07 #1
1 2275
Is there a shortcut to pull all columns from both data tables?

How about:
select new { Employee = rowEmpData, Note = rowNoteData };

This gives you all the data, without having to worry about name
conflicts like "EmployeeID" in each. It is also clear what comes from
where - essentially acting as the alias in SQL:

foreach (var result in results) {
Console.WriteLine("{0}, {1}, {2}",
result.Employee["EmployeeID"], result.Employee["Salary"],
result.Note["Notes"]);
}

Any use?

Marc
Dec 9 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris | last post by:
I am a little confused. I have been reading about LINQ and it seems to imply LINQ is available in C# 3 but not in Visual Studio until the next release. I am a VB.net programmer but would still like...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
4
by: BeSharp | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). ...
13
by: Dan Tallent | last post by:
I have a query which I would like to convert to LINQ. I am having problems finding good examples or references that cover more complex queries. Here is the SQL command I would like to rewrite...
9
by: Cirene | last post by:
I'm about to begin a brand new, big, ASP.NET project (using 3.5 .net fw), VS 2008. I'm using MySQL as the backend (customer request.) I have absolutely no experience with LINQ and/or the Entity...
20
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
21
by: hrishy | last post by:
Hi Will LINQ be ported to Python ? regards Hrishy
1
by: hrishy | last post by:
Hi Well wouldn't it be a lot easier to query and join a xml source with a relational source with LINQ capabilites in Python. Hmm what am i missing here is there a site that takes all LINQ...
4
by: CSharper | last post by:
I have following XML <root> <Person id="1"> <Name>a</Name> </Person> <Person id="2"> <Name>b</Name> </Person> </root>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.