嗨我有以下代码来Customers
Advertureworks2008R2LT
和SalesPerson
对客户'进行分组last name
。 var dc = new DataClasses1DataContext();
var groupedCust = (from c in dc.Customers
group c by new { c.SalesPerson, c.LastName }).Take(10);
foreach (var item in groupedCust)
{
Console.WriteLine("\n***{0}***", item.Key.SalesPerson);
Console.WriteLine("==========================");
Console.WriteLine("\n{0}", item.Key.LastName);
Console.WriteLine("-------------------------");
foreach (var item2 in item)
{
Console.WriteLine("{0}, {1}", item2.LastName, item2.FirstName);
}
}
。
LINQ2SQL
我正在使用"adventure-works\shu0"
仅用于演示。问题是它没有与名为adventure-works\shu0
的销售人员进行分组,在"adventure-works\linda3"
之间存在名为linda3
的销售人员。但这不应该发生,因为shu0
必须在所有***adventure-works\pamela0***
==========================
Gee
-------------------------
Gee, Orlando
Gee, Orlando
***adventure-works\david8***
==========================
Harris
-------------------------
Harris, Keith
Harris, Keith
***adventure-works\jillian0***
==========================
Carreras
-------------------------
Carreras, Donna
Carreras, Donna
***adventure-works\jillian0***
==========================
Gates
-------------------------
Gates, Janet
Gates, Janet
***adventure-works\shu0***
==========================
Harrington
-------------------------
Harrington, Lucy
Harrington, Lucy
***adventure-works\linda3***
==========================
Carroll
-------------------------
Carroll, Rosmarie
Carroll, Rosmarie
***adventure-works\shu0***
==========================
Gash
-------------------------
Gash, Dominic
Gash, Dominic
***adventure-works\josé1***
==========================
Garza
-------------------------
Garza, Kathleen
Garza, Kathleen
***adventure-works\josé1***
==========================
Harding
-------------------------
Harding, Katherine
Harding, Katherine
***adventure-works\garrett1***
==========================
Caprio
-------------------------
Caprio, Johnny
Caprio, Johnny
之后。为什么会发生这种情况,我该如何解决这个问题?
这是查询执行的结果:
num_vertices, num_edges = gets.chomp.split(' ').map { |e| e.to_i }
graph = Graph.new
(1..num_vertices).to_a.each do |vertex|
graph.add_node_by_val(vertex)
end
num_edges.times do |edge|
first, second = gets.chomp.split(' ').map { |e| e.to_i }
graph.add_edge_by_val(first, second, 0, false)
end
even_edges = 0
graph.edges.each do |edge|
dup = graph.deep_dup
first_tree = nil
second_tree = nil
subject_edge = nil
dup.edges.each do |e|
if e.first.value == edge.first.value && e.second.value == edge.second.value
subject_edge = e
first_tree = e.first
second_tree = e.second
end
end
dup.remove_edge(subject_edge)
if first_tree.size.even? && second_tree.size.even?
even_edges += 1
end
end
puts even_edges
答案 0 :(得分:3)
这里的问题是,与LINQ to Objects的处理方式相比,LINQ to SQL / LINQ to Entities如何处理分组之间存在差异。
基本上,当您在L2S / L2E中使用groupby
子句(或GroupBy
方法)时,将从生成的SQL中删除排序。结果是SQL服务器将以您无法控制的任意顺序返回结果...除非您在组操作后指定排序。
var groupedCust =
(
from c in dc.Customers
group c by new { c.SalesPerson, c.LastName } into grp
orderby grp.Key.SalesPerson, grp.Key.LastName
select grp
).Take(10);
这应该产生你所追求的输出,但是在LINQ to SQL中查询组时会有一个警告。当您随后枚举结果时,LINQ将为具有组键特征的每个组发出查询,以获取与该组对应的记录。这意味着多次往返数据库,而不是仅仅开始一堆记录。
要从数据库中获取所需的记录(只有您想要的记录),您可以加入组查询,获取匹配的记录,然后将它们重新组合到内存中。
或者把它放在代码中:
// when enumerated this will result in one SQL statement
var groupingSource =
(
from key in
(
from gc in dc.Customers
group 1 by new { gc.SalesPerson, gc.LastName } into grp
orderby grp.Key.SalesPerson, grp.Key.LastName
select grp.Key
).Take(10)
join c in dc.Customers on key equals new { c.SalesPerson, c.LastName }
select c
);
// get the records and re-group them
var groupedCust =
from c in groupingSource.AsEnumerable()
group c by new { c.SalesPerson, c.LastName } into grp
orderby grp.Key.SalesPerson, grp.Key.LastName
select grp;
其中的AsEnumerable
会将记录从数据库中拉入内存,并将表达式的其余部分作为LINQ to Objects ...来完成,只需一次调用数据库即可获得预期的结果而不是11个单独的电话。当你从数据库中删除 lot 组时它会产生真正的不同,请相信我。
答案 1 :(得分:0)
您获得的输出对于您已经给出的LINQ / SQL来说非常好。
但试试这个,看看你是否得到了你想要的结果:
var groupedCust =
(
from c in dc.Customers
orderby c.LastName
orderby c.SalesPerson
group c by new { c.SalesPerson, c.LastName }
).Take(10);