Twistar
Twistar

Reputation: 782

Linq to SQL joining two tables and populate GridView

I have two tables in my database, Building and Town. They look like this:

Building:

buildingid
buildingname

Town:

id
userid
buildingid

In Town there is one entry for each building a user has.

What i want is to populate a GridView for a user with a given userid. This GridView should include the buildingname and the number of buildings. Building. I have tried this:

var buildings = (from Town in dc.Towns
                         join Building in dc.Buildings
                             on Town.buildingid equals Building.buildingid
                         select Building.buildingname);
        gvBuildings.DataSource = buildings;
        gvBuildings.DataBind();

But I don't know how to get the numbers for each building.


I have now been working on this for a while and a couple of your answers work. I have used this code:

var buildings = dc.Towns
            .Where(t => t.userid == userid)
            .GroupJoin(dc.Buildings,
                       t => t.buildingid,
                       b => b.buildingid,
                       (Town, Buildings) => new
                                                {
                                                    BuildningName = Buildings.First().buildingname,
                                                    Count = Buildings.Count()
                                                });
        gvBuildings.DataSource = buildings.ToList();
        gvBuildings.DataBind();

When i run this code my GridView ends up looking like this: GridView

I need the buildings to be shown in groups, grouped by the buildingname. I have tried all of the suggestions but i cant get it to work.

Upvotes: 0

Views: 4727

Answers (4)

Dipen
Dipen

Reputation: 11

 manDbDataContext db = new DbDataContext();

 var estimatedTotal = (  from est in db.AssignmentEstimatedMaterials
                         where est.assignment_id == Convert.ToInt32(Label_assignmentId.Text)
                         join materialdetail in db.Materials on est.material_id equals materialdetail.material_id
                         select new { est.qty,est.total_amount, materialdetail.material_name}).ToList();

    GridView_estiamte_material.DataSource = estimatedTotal;
    GridView_estiamte_material.DataBind();

Note, you should select individual data and it works.

Upvotes: 1

ajay swamy
ajay swamy

Reputation: 271

 var buildings = (from Town in dc.Towns
                                 join Building in dc.Buildings
                                     on Town.buildingid equals Building.buildingid
                                 into results
                                 from r in results.DefaultIfEmpty()
                                 group Town by new 
                                 {
                                     r.BuildingId
                                 } into groupedResults
                                 where Town.UserID == parameteruserId
                                 select new 
                                 {
                                     BuildingName = Building.buildingname,
                                     BuildingCount = groupedResults.Count()
                                  });

Try this.. it should work.. i have a similar requirement..

Upvotes: 1

Pranay Rana
Pranay Rana

Reputation: 176896

check linq differed execution

and than try the blow code might work for you

    var buildings = 

(from j in dc.Town 
join i in dc.Buildings
 on j.buildingId equals i.buildingId
where j.Userid = varUSerid 
group new {i, j} 
by new 
{   i.BuildingID } 
into 
g  
select new {   
  BuildingName = g.First<k=>k.BuildingName)
   , count = g.Count() } ).ToList();
    gvBuildings.DataSource = buildings; 
          gvBuildings.DataBind(); 

Upvotes: 1

RePierre
RePierre

Reputation: 9566

Try grouping:

var buildings = dc.Towns
                  .Where(t => t.UserId == userId)
                  .GroupJoin(dc.Buildings,
                             t => t.BuildingId,
                             b => b.BuildingId,
                             (town, buildings) => new
                             {
                                 BuildingName = buildings.First().BuildingName,
                                 Count = buildings.Count
                             });

Keep in mind that when binding to a control you must supply a collection of type (or implementing) IList. This can be accomplished by calling ToList() on the buildings collection:

gvBuildings.DataSource = buildings.ToList();
gvBuildings.DataBind();

Upvotes: 1

Related Questions