0

I have Calls table:

Call(id, reason_id, company_id, text)   

I have Reason table:

Reason(id, name)  

I have Company table:

Company(id, name)  

Calls has a foreign key to Reason and Company

I am using Entity Framework 4 and I would like to display a list of calls and for each call display the text, reason name and company name.

Something like an inner join.

I have added the tables to the edmx file. how can I get the data I need? which POCO object will hold the external data (company name and reason name)?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Naor
  • 23,465
  • 48
  • 152
  • 268

2 Answers2

0

Try this:

using (YourEntities ctx = new YourEntities())
{
    //In this example, "Reason" and "Company" are the name of
    //your navigation properties
    return ctx.Calls.Include("Reason").Include("Company").ToList();
}

This will give you a List<Call>. So each Call will have Call.Reason.Name or something similar depending on your model.

BrandonZeider
  • 8,014
  • 2
  • 23
  • 20
  • In case I have lazy loading, does I need to do Include? – Naor Apr 21 '11 at 01:09
  • Company actually has a lot of data but I need only the company name. Moreover - I need to pass the data to the client and I need as less data as possible. – Naor Apr 21 '11 at 01:24
  • Entity Framework doesn't allow you to specify which columns to retrieve and which ones to skip. Lazy loading is only going to work as long as their is an active connection, so you're going to want to use the .Include, hydrate the objects, then pass them to the client. If you want them to be as thin as possible, remove Lazy Loading and use POCO objects. – BrandonZeider Apr 21 '11 at 01:49
0

Maybe you can use the concept of ViewModel:

 //Create  CallViewModel contains ReasonName & CompanyName
    public class CallViewModel
    {
        public int id { get; set; }
        public string Text{ get; set; }
        public string ReasonName { get; set; }
        public string CompanyName { get; set; }
    }

public List<CallViewModel> YourMethid()
{
    using (Entities _entities = new Entities())
    {
        var result = (from s in _entities.Call.Include("Reason").Include("Company")
                      select new CallViewModel
                          {
                              id = s.id,
                              Text = s.Text,
                              CompanyName = s.Company.name,
                              ReasonName = s.Reason.name
                          }).ToList();
        return result;
    }
}

======================= //select all data from _entities Call Table

var result = (from s in _entities.Call.Include("Reason").Include("Company") select s).ToList();

//get first data's ReasonName

var ReasonName = result[0].Reason.ReasonName;

//get first data's CompanyName

var CompanyName = result[0].Company.CompanyName;

Maidot
  • 386
  • 4
  • 11
  • @Maidot: And where should I put the values? which POCO will hold the additional info? – Naor Apr 21 '11 at 01:11
  • Naor: what kind of control you use to display the data? yae POCO Entities will hold the value if you use "Include"~ – Maidot Apr 21 '11 at 01:19
  • @Naor: Sorry it's my bad, Lazy Loading may not work with "Include" – Maidot Apr 21 '11 at 01:30
  • @Maidot: See the comment I wrote to BrandonZeider. I need as less data as can. – Naor Apr 21 '11 at 01:37
  • @Naor:Why not use the Ajax to get the info what you want? – Maidot Apr 21 '11 at 04:54
  • @Maidot: What is the difference between Include and joins in linq in order to get the additional data? – Naor Apr 21 '11 at 09:35
  • Sorry, I don't know how to explain the diffrernt between include and Join, bot I think those information can help you:[LINQ to Entities (Entity Framework) Join and .Include conflict](http://stackoverflow.com/questions/794283/linq-to-entities-include-method-not-loading) ,[Join and Include in Entity Framework](http://stackoverflow.com/questions/416847/join-and-include-in-entity-framework) . – Maidot Apr 21 '11 at 10:02