0

I'm using Linq and just started working with it. My gridview was populating when I was using a single table, but now I am trying to join a Client First Name from the Client Info table.

Error is Event_Setup does not contain a definition for ClientFirstName. So it is checking Event_Setup table instead of Client_Info.

public List<EventData> GetDetails()
    {
        using (EMSEntities db = new EMSEntities())
        {
            var context = from events in db.Event_Setup
                          join clients in db.Client_Info on events.ClientId equals clients.ClientId 
                        select events;

            List<EventData> newEvent = new List<EventData>();

            foreach (var e in context)
            {
                EventData test = new EventData();

                test.Event_Title = e.EventTitle;

//Error on e.ClientFirstName, Event Setup does not contain Definition
 (located in Client_Info table not Event Setup)
                test.Name = e.ClientFirstName;
                test.Start_Date = e.EventDateFrom;
                test.End_Date = e.EventDateFrom;



               newEvent.Add(test);                
            }
            return newEvent;
         }         
    }

DAL

public class EventData
{        
        public string Event_Title { get; set; }
        public string Name { get; set; }
        public DateTime? Start_Date { get; set; }
        public DateTime? End_Date { get; set; }            
}
4

2 回答 2

1

此代码没有对连接的clients数据做任何事情。您可以看到它只是在选择events

var context = from events in db.Event_Setup
              join clients in db.Client_Info on events.ClientId equals clients.ClientId 
              select events;

一旦你摆脱了那个 LINQ 语句,它就clients消失了。它不会为您合并任何数据。你需要做一个投影,像这样:

var context = from events in db.Event_Setup
              join clients in db.Client_Info on events.ClientId equals clients.ClientId 
              select new EventData
              {
                  Event_Title = events.EventTitle,
                  Name = clients.ClientFirstName,
                  Start_Date = events.EventDateFrom,
                  End_Date = events.EventDateFrom
              };
于 2013-08-22T19:10:11.607 回答
0

谢谢你的Cory,这是查看此问题的任何人想要的列表

foreach (var e in context)
            {
                EventData test = new EventData();

                test.Event_Title = e.Event_Title;
                test.Name = e.Name;
                test.Start_Date = e.Start_Date;
                test.End_Date = e.End_Date;           

                newEvent.Add(test);                
            }
            return newEvent;
于 2013-08-22T19:27:25.027 回答