0

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

建造:

buildingid
buildingname

镇:

id
userid
buildingid

在 Town 中,用户拥有的每座建筑物都有一个入口。

我想要的是为具有给定用户 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 最终看起来像这样: 网格视图

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

4

4 回答 4

1

检查linq 不同的执行

而不是尝试打击代码可能对你有用

    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(); 
于 2012-04-25T07:44:47.530 回答
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()
                                  });

试试这个..它应该可以工作..我有类似的要求..

于 2012-04-25T07:47:46.113 回答
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();
于 2012-04-25T08:08:25.593 回答
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();

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

于 2012-11-01T11:32:30.057 回答