Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
SolvedPrivate

Select multiple tables in LINQ

Posted on 2014-09-04
5
37 Views
Last Modified: 2016-02-15
Hi Experts,

I have this SQL query statement:

SELECT d1.*
FROM FinanceDept d1
INNER JOIN
(SELECT User_ID, MAX(SalesAmt) AS 'Amount' FROM SalesDept GROUP BY User_ID) d2
ON d1.User_ID = d2.User_ID AND d1.Amount= d2.Amount

But I don't know how to write it in LINQ and Lambda expression.
Please help.

Thank you
0
Comment
Question by:alexcryout
  • 2
  • 2
5 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 40305509
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 40305938
Hi alexcryout;

Try this query and see if it returns the required results.

var results = from d1 in FinanceDept
              join d2 in SalesDept on new { d1.User_ID, d1.Amount  } equals new { d.OrderID, d2.Amount } into userGroup
              orderby d1.User_ID
              select new 
              {
                 FinanceDept = d1,
                 User_ID_d2 = userGroup.Select( u => u.User_ID ).FirstOrDefault(),
                 Amount = userGroup.Max( a = a.SalesAmt )
              };

Open in new window

0
 

Author Comment

by:alexcryout
ID: 40306230
Hi Fernando,

Sorry for my typo, I am trying to join the same table. The correct SQL statement is:

SELECT d1.*
 FROM FinanceDept d1
 INNER JOIN
 (SELECT User_ID, MAX(SalesAmt) AS 'Amount' FROM FinanceDept GROUP BY User_ID) d2
 ON d1.User_ID = d2.User_ID AND d1.Amount= d2.Amount

Thanks
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40306311
Hi alexcryout;

Just substitute the table name SalesDept with FinanceDept as shown in this line of the query and that should do it.

(SELECT User_ID, MAX(SalesAmt) AS 'Amount' FROM FinanceDept GROUP BY User_ID) d2
0
 

Author Closing Comment

by:alexcryout
ID: 40306382
It works fine but I download the Linqer tool and its solution is much better.

Thank you for all your helps.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

808 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