Solved

LINQ to SQL

Posted on 2013-10-23
6
248 Views
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"])

                            }).Distinct().ToList();
0
Comment
Question by:countrymeister
  • 3
  • 2
6 Comments
 
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.
0
 
LVL 1

Author Comment

by:countrymeister
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
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39594812
How many rows are you getting back?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:countrymeister
ID: 39594822
I am getting 4890 rows back
0
 
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?
0
 
LVL 74

Accepted Solution

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

e.g.

 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,

               }).ToList();

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 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

18 Experts available now in Live!

Get 1:1 Help Now