Linq to SQL连接两个表并填充GridView

时间:2012-04-25 07:42:45

标签: c# asp.net linq-to-sql gridview join

我的数据库中有两个表,Building和Town。它们看起来像这样:

大厦:

buildingid
buildingname

镇:

id
userid
buildingid

在城镇中,每个建筑物都有一个条目。

我想要的是为具有给定用户ID的用户填充GridView。此GridView应包含建筑物名称和建筑物数量。 建造。我试过这个:

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();

但我不知道如何获得每栋楼的数字。


我现在已经做了一段时间了,你的几个答案都有效。我使用过这段代码:

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();

当我运行此代码时,我的GridView最终看起来像这样: GridView

我需要将建筑物分组显示,按建筑物名称分组。我已经尝试了所有的建议,但我无法让它发挥作用。

4 个答案:

答案 0 :(得分:1)

检查linq differed execution

而不是尝试打击代码可能适合你

    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(); 

答案 1 :(得分:1)

 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()
                                  });

试试这个..它应该有用..我有类似的要求..

答案 2 :(得分:1)

尝试分组:

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
                             });

请记住,绑定到控件时,您必须提供类型(或实现)IList的集合。这可以通过致电ToList()集合上的buildings来完成:

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

答案 3 :(得分:1)

 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();

注意,您应该选择单个数据并且它可以正常工作。