简体   繁体   English

Linq to SQL分组子关系

[英]Linq to SQL Grouping Child Relationships

I'm trying to run a LINQ to SQL query that returns a result in a grid view in a search engine style listing. 我正在尝试运行LINQ to SQL查询,该查询在搜索引擎样式列表的网格视图中返回结果。

In the simplified example below, is it possible to populate the collection with a comma-separated list of any children that the parent has (NAMESOFCHILDREN) in a single query? 在下面的简化示例中,是否可以在单个查询中用逗号分隔的列表填充父级拥有的任何子级(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 />"
             };

Thanks in advance. 提前致谢。

Your joins are going to screw up your cardinality! 您的加入将加深您的基数! You don't have a list of Parents! 您没有父母名单!

Here's some untested free-hand code. 这是一些未经测试的徒手代码。 Adding the relationships in the Linq designer gives you relationship properties. 在Linq设计器中添加关系将为您提供关系属性。 String.Join will put the list together. String.Join会将列表放在一起。

I've added two optional method calls. 我添加了两个可选的方法调用。

Where ... Any will filter the parents to only those parents that have children. 凡……任何人都会过滤父母,使其仅过滤那些有孩子的父母。 I'm unsure of string.Join's behavior on an empty array. 我不确定string.Join在空数组上的行为。

ToList will yank Parents into memory, the children will be accessed by further database calls. ToList将把Parents拖入内存,进一步的数据库调用将访问孩子。 This may be necessary if you get a runtime string.Join is not supported by SQL translator exception. 如果您获得运行时字符串,则可能有必要。SQL转换程序异常不支持联接 This exception would mean that LINQ tried to translate the method call into something that SQL Server can understand - and failed. 此异常意味着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();

Also note: generally you do not want to mix data and markup until the data is properly escaped for markup. 还要注意:通常,您不希望将数据和标记混合在一起,直到为标记正确地转义了数据为止。

you could try as follow: 您可以尝试如下操作:

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

Posting an answer for an old question with groupby . groupby发布一个老问题的答案。 Below query will produce company name, order count and order ids separated by comma from Northwind. 在下面的查询中,将产生公司名称,订单数和订单ID,并以逗号分隔Northwind。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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