?
Solved

Need LINQ Lambda Entity Frameworks

Posted on 2014-02-15
3
Medium Priority
?
528 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:Charles Baldo
  • 2
3 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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:Charles Baldo
ID: 39862273
Thank you kindly
0
 
LVL 64

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

850 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