C# Linq到SQL分组子关系

C# Linq到SQL分组子关系,c#,.net,linq,linq-to-sql,C#,.net,Linq,Linq To Sql,我正在尝试运行一个LINQtoSQL查询,该查询在搜索引擎样式列表的网格视图中返回结果 在下面的简化示例中,是否可以用一个逗号分隔的列表填充集合,其中包含父项在单个查询中拥有的任何子项(NAMESOFCHILDREN) var family = from p in db.Parents where p.ParentId == Convert.ToInt32(Request.QueryString["parentId"]) join pcl in

我正在尝试运行一个LINQtoSQL查询,该查询在搜索引擎样式列表的网格视图中返回结果

在下面的简化示例中,是否可以用一个逗号分隔的列表填充集合,其中包含父项在单个查询中拥有的任何子项(NAMESOFCHILDREN)

var family = from p in db.Parents
             where p.ParentId == Convert.ToInt32(Request.QueryString["parentId"])
             join pcl in db.ParentChildLookup on p.ParentId equals pcl.ParentId
             join c in db.Children on pcl.ChildId equals c.ChildId
             select new
             {
                 Family = "Name: " + p.ParentName + "<br />" + 
                          "Children: " + NAMESOFCHILDREN? + "<br />"
             };
var family=来自p,单位为db.Parents
其中p.ParentId==Convert.ToInt32(Request.QueryString[“ParentId”])
在p.ParentId上的db.ParentChildLookup中加入pcl等于pcl.ParentId
在数据库中加入c。pcl.ChildId上的Children等于c.ChildId
选择新的
{
Family=“Name:”+p.ParentName+“
”+ “儿童:“+NAMESOFCHILDREN?+”
” };

提前感谢。

您的加入将破坏您的基数!你没有父母的名单

这里有一些未经测试的免费代码。在Linq设计器中添加关系将为您提供关系属性。Join将把列表放在一起

我添加了两个可选的方法调用

在哪里。。。Any将仅将父对象筛选到有子对象的父对象。我不确定string.Join在空数组上的行为

ToList将把父母拉入内存,孩子们将通过进一步的数据库调用进行访问。如果获取运行时字符串,则可能需要执行此操作。SQL translator异常不支持联接。此异常意味着LINQ试图将方法调用转换为SQL Server能够理解的内容,但失败了

int parentID = Convert.ToInt32(Request.QueryString["parentId"]);

List<string> result =
  db.Parents
  .Where(p => p.ParentId == parentID)
  //.Where(p => p.ParentChildLookup.Children.Any())
  //.ToList()
  .Select(p => 
    "Name: " + p.ParentName + "<br />" + 
    "Children: " + String.Join(", ", p.ParentChildLookup.Children.Select(c => c.Name).ToArray() + "<br />"
)).ToList();
intparentid=Convert.ToInt32(Request.QueryString[“parentID]”);
列表结果=
db.父母
.Where(p=>p.ParentId==ParentId)
//.Where(p=>p.ParentChildLookup.Children.Any())
//托利斯先生()
.选择(p=>
名称:“+p.ParentName+”
“+ “Children:”+String.Join(“,”,p.ParentChildLookup.Children.Select(c=>c.Name).ToArray()+”
” )).ToList();

另请注意:通常,在正确转义数据以进行标记之前,您不希望混合数据和标记。

您可以尝试以下操作:

var family = from p in db.Parents            
     where p.ParentId == Convert.ToInt32(Request.QueryString["parentId"])             
    join pcl in db.ParentChildLookup on p.ParentId equals pcl.ParentId             
     select new             {                 
        Family = "Name: " + p.ParentName + "<br />" + string.Join(",",(from c in db.Children where c.ChildId equals pcl.ChildId  select c.ChildId.ToString()).ToArray());
    };
var family=来自p,单位为db.Parents
其中p.ParentId==Convert.ToInt32(Request.QueryString[“ParentId”])
在p.ParentId上的db.ParentChildLookup中加入pcl等于pcl.ParentId
选择新{
Family=“Name:”+p.ParentName+“
“+string.Join(“,”,(从db.Children中的c开始,其中c.ChildId等于pcl.ChildId选择c.ChildId.ToString()).ToArray()); };
groupby
发布一个老问题的答案。下面的查询将从Northwind生成公司名称、订单数量和订单ID,以逗号分隔

var query = from c in north.Customers
                    join o in north.Orders on c.CustomerID equals o.CustomerID
                    select new { c, o };

        var query2 = from q in query
                     group q.o by q.c into g
                     select new { CompanyName = g.Key.CompanyName, 
                                orderCount = g.Count(), 
                                orders = string.Join(",", g.Select(o => o.OrderID)) }
                     into result
                         orderby result.orderCount descending
                     select result;

谢谢我决定以不同的方式显示这些数据,但这让我朝着正确的方向前进。