Linq with entity framework

c# entity-framework linq

Question

I'm new to LINQ and Entity Framework and would appreciate some advice on the following scenario.

I have a entity model with two tables.

Travel_Request and Resource

Sample Fields

Travel_Request

Request_ID
Resource_ID

Resource

Resource_ID
Resource_Name

I would like to add the Resource_Name to the list when returning all the TRAVEL_REQUESTS

Thanks in advance

1
3
9/4/2013 2:50:33 PM

Accepted Answer

Hi you need to use the Linq join:

var data = from t in Travel_Request
       join r in Resource on t.Resource_ID equals r.Resource_ID
       select new 
              { 
                 RequestId = t.Request_ID,
                 ResourceId = t.Resource_ID,
                 ResourceName = r.Resource_Name
              };

If you already have an EF association then it could simply be:

var data = from t in Travel_Request
    select new
           {
                 RequestId = t.Request_ID,
                 ResourceId = t.Resource_ID,
                 ResourceName = t.Resource.Resource_Name
           };
7
9/4/2013 3:06:26 PM

Popular Answer

You will have to create a new object something like this.

var data = Travel_Request.Select(s=> new { Resource_Name = s.Recource.Resource_Name, Request_ID = s.Request_ID, Resource_ID = s.Resource_ID}).ToList();

As long as I've understood the question correctly this will work.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow