开发者

display additional data to table fields - entity framework 4

I ha开发者_高级运维ve 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)?


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;


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜