Posted on 2013-10-23
Last Modified: 2016-02-10
I have two datatables T1 and T2
I neeed to do a join but only get the first or default from T2
Because T2 could have multiple rows that match the join condition

I tried this
 var result = (from dataRows1 in T1.AsEnumerable()
                            join dataRows2 in T2.AsEnumerable()
                            on dataRows1["ID"] equals dataRows2["ID"]
                            select new Returns
                                ID = dataRows1["ID"],
                                Date = dataRows1["Date"],
                                StartPrice = Convert.ToDecimal(dataRows2["StartPrice"]),
                                EndPrice = Convert.ToDecimal(dataRows2["EndPrice"])

Question by:countrymeister
  • 3
  • 2
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 39594749
Hi countrymeister;

You state the following, "I need to do a join but only get the first or default from T2", it is possible the the other lines are getting filtered out because of the use of the Distinct method call in the query. To test remove the Distinct() and leaving everything else in place.

Author Comment

ID: 39594797
I removed the distinct that did not help
t1 has 2550 rows, and T2 has 4890 rows, I just want the 2550 rows back in my join

Example t2 could have multiple rows with ID matching the ID in T1
LVL 62

Expert Comment

by:Fernando Soto
ID: 39594812
How many rows are you getting back?
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


Author Comment

ID: 39594822
I am getting 4890 rows back
LVL 62

Expert Comment

by:Fernando Soto
ID: 39594970
I have tried similar Linq query and it seems to work on my systems as expected.

Running the query using the Distinct() method and you tell me the number of records returned?

Also can you post a screen shot of the results where data is missing?
LVL 75

Accepted Solution

käµfm³d   👽 earned 400 total points
ID: 39596299
Add a group by to the mix:


 var result = (from dataRows1 in T1.AsEnumerable()
               join dataRows2 in T2.AsEnumerable()
               on dataRows1["ID"] equals dataRows2["ID"]
               group new { ID = dataRows1["ID"], Date = dataRows1["Date"], StartPrice = dataRows2["StartPrice"], EndPrice = dataRows2["EndPrice"] }
               by dataRows1["ID"] into g
               let first = g.FirstOrDefault()
               let firstDate = (first == null ? (DateTime)null : Convert.ToDateTime(dataRows1["Date"]))
               let firstStartPrice = (first == null ? 0M : Convert.ToDecimal(dataRows2["StartPrice"]))
               let firstEndPrice = (first == null ? 0M : Convert.ToDecimal(dataRows2["EndPrice"]))
               select new Returns
                   ID = g.Key,
                   Date = firstDate,
                   StartPrice = firstStartPrice,
                   EndPrice = firstEndPrice,


Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now