简体   繁体   中英

Show on gridview data from two tables LINQ

i have two tables: 1. Clients: clientID, Name, Surname, Address, 2. Visit: visitID, clientID, Date, Place. I want display on GridView list with visits details: Name,Surname,Address,Date,Place.

I have something like that but it not working correctly, it's only showing the data from Visit table:

using (NewEntities dc = new NewEntities())
            {
                var visits = (from a in dc.Visits
                              join b in dc.Clients on a.clientID equals b.clientID
                              select new 
                              {
                                  a,
                                  b.Name,b.Surname,b.Address,

                              });

                if (visits != null)
                {
                    allVisits = new List<Visits>();
                    foreach (var i in visits)
                    {
                            Visits c = i.a;

                        allVisits.Add(c);
                    }
                }

                if ((allVisits == null) || (allVisits.Count == 0))
                {
                    allVisits.Add(new Visits());
                    myGridView.DataSource = allVisits;
                    myGridView.DataBind();
                    myGridView.Rows[0].Visible = false;

                }
                else
                {
                    myGridView.DataSource = allVisits;
                    myGridView.DataBind();
                }

            }
        }

Try binding the visits variable, as it seems like that's the data you want.

using (NewEntities dc = new NewEntities())
{
    var visits = (from a in dc.Visits
              join b in dc.Clients on a.clientID equals b.clientID
              select new 
              {
                  a,
                  b.Name,b.Surname,b.Address,
              });

    myGridView.DataSource = visits.ToList();
    myGridView.DataBind();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM