[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

LINQ to SQL

Posted on 2013-10-23
6
Medium Priority
?
277 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 400 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 64

Expert Comment

by:Fernando Soto
ID: 39594812
How many rows are you getting back?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:countrymeister
ID: 39594822
I am getting 4890 rows back
0
 
LVL 64

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 75

Accepted Solution

by:
käµfm³d   👽 earned 1600 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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 video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

873 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