是否有更好的解决方案来执行Linq组加入?

时间:2013-11-26 20:52:49

标签: c# linq

我有两个非常大的表,它们都有超过10K的重新编码。我需要使用Linq从这两个表中查询。见下面的例子,你可以看到我需要使用组连接。

enter image description here

以下是我的查询,它有效。但它需要大约6-7分钟才能完成。 TableA有超过10K的记录。 TableB比tableA有更多的记录。

from a in tableA
join b in tableB on new {ID = a.OrderID, Name = a.Option} 
             equals new{ID = b.OrderID, Name = b.SelectedOption} into jgroup
select new{
    OrderID = a.OrderID,
    SelectedOption = a.Option,
    SelectedOptionValue = 
             jgroup.Select(g => g.SelectedOptionValue).SingleOrDefault()
}

使用linq to SQL是否有更好的解决方案可以加快速度。

任何建议将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

linq语句可能无法进行最佳翻译。我认为更明确的左连接语法最适合

var query = 
  from a in tableA
  from b in tableB.Where(x => a.OrderID == x.OrderID && a.Option == x.SelectedOption).DefaultIfEmpty()
  select new
  {
    OrderID = a.OrderID,
    SelectedOption = a.Option,
    SelectedOptionValue = b.SelectedOptionValue
  };