Solved

Need LINQ Lambda Entity Frameworks

Posted on 2014-02-15
3
508 Views
Last Modified: 2016-02-10
I am new to LINQ and Entity Frameworks.  I am trying to figure out how to do a join in LINQ and return data in a list.  I am using REST webservices.

I have the following  SQL that returns 18 rows

SELECT
   ClientApp.ClientId,
   ClientApp.AppId,
   ClientApp.ClientAppExpirationDate,
   App.Description
FROM
   ClientApp INNER JOIN App ON ClientApp.AppId = App.AppId
where
   ClientApp.ClientId=1

-----------------------------------
With the help of a colleague I have the following Lambda LINQ that returns the same number of rows in two lists the dates and descriptions match in the same rows. Only problem is I need one list.  Short of creating a third list and going through and putting the data in the row from each list into the new list I am hoping a single LINQ query with the same join can do the job.

using (var context = new  WSContext())
{
//   List<AppList> appList = new List<AppList>();
List<ClientApp> clientAppList = new List<ClientApp>();
List<App> appList = new List<App>();
var app = context.ClientApps.Where(c => c.ClientId == _clientID).Join(context.Apps, ca => ca.AppId, a => a.AppId, (ca, a) =>
      new { clientApp = ca, apps = a });
var resultApps = app.Select(s => s.apps);
var resultClientApp = app.Select(a => a.clientApp );

clientAppList = resultClientApp.ToList();
appList = resultApps.ToList();

-----------------------

}
0
Comment
Question by:charlesbaldo
  • 2
3 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39862238
Hi charlesbaldo;

You were not that far off. This code should do what you need.

using (var context = new  WSContext())
{
	   var resultList = context.ClientApp
	                           .Where(c => c.ClientId == _clientID)
	                           .Join(context.Apps, 
	                                 ca => ca.AppId, 
	                                 a => a.AppId, 
	                                 (ca, a) =>  new 
	                                     { 
	                                         ClientId = ca.ClientId,
	                                         AppId = caa.AppId,
	                                         ClientAppExpirationDate = ca.ClientAppExpirationDate,
	                                         Description = a.Description	                                         
	                                     }
	                                 );
}

Open in new window

0
 

Author Closing Comment

by:charlesbaldo
ID: 39862273
Thank you kindly
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39862752
Not a problem Charles, glad to help.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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