Solved

Need LINQ Lambda Entity Frameworks

Posted on 2014-02-15
3
511 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
kill all other existing SQL server connection except the login started by current session 65 99
Syntax Error 2 43
Angular JS Route 3 45
SQL Exceptions 3 36
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 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

14 Experts available now in Live!

Get 1:1 Help Now