LINQ 左外连接语法差异
在 LINQ 中至少有两种执行 LEFT OUTER JOIN 的方法
class Customer
{
public int ID { get; set; }
public string Name { get; set; }
}
class Order
{
public int ID { get; set; }
public string Product { get; set; }
}
static void Main()
{
// Example customers.
var customers = new Customer[]
{
new Customer{ID = 5, Name = "Sam"},
new Customer{ID = 6, Name = "Dave"},
new Customer{ID = 7, Name = "Julia"},
new Customer{ID = 8, Name = "Sue"},
new Customer{ID = 9, Name = "Joe"}
};
// Example orders.
var orders = new Order[]
{
new Order{ID = 5, Product = "Book"},
new Order{ID = 6, Product = "Game"},
new Order{ID = 7, Product = "Computer"},
new Order{ID = 8, Product = "Shirt"},
new Order{ID = 8, Product = "TShirt"}
};
// First Way
var query = from c in customers
join o in orders on c.ID equals o.ID into co
from x in co.DefaultIfEmpty()
where x != null && x.Product == "Shirt"
select new {c, x};
// Second Way
var query2 = from c in customers
from o in orders
where c.ID == o.ID && o.Product == "Shirt"
select new {c, o};
}
我发现了很多第一种方法的示例(使用“into”),这就是我用来执行 LEFT OUTER JOINS 的方法。最近发现了第二种方法,看起来更简单。我测试过,它们都产生相同的结果。现在我的问题是有任何隐藏的差异、性能,还是只是语法糖?
多谢。
There are at least two ways of doing LEFT OUTER JOIN in LINQ
class Customer
{
public int ID { get; set; }
public string Name { get; set; }
}
class Order
{
public int ID { get; set; }
public string Product { get; set; }
}
static void Main()
{
// Example customers.
var customers = new Customer[]
{
new Customer{ID = 5, Name = "Sam"},
new Customer{ID = 6, Name = "Dave"},
new Customer{ID = 7, Name = "Julia"},
new Customer{ID = 8, Name = "Sue"},
new Customer{ID = 9, Name = "Joe"}
};
// Example orders.
var orders = new Order[]
{
new Order{ID = 5, Product = "Book"},
new Order{ID = 6, Product = "Game"},
new Order{ID = 7, Product = "Computer"},
new Order{ID = 8, Product = "Shirt"},
new Order{ID = 8, Product = "TShirt"}
};
// First Way
var query = from c in customers
join o in orders on c.ID equals o.ID into co
from x in co.DefaultIfEmpty()
where x != null && x.Product == "Shirt"
select new {c, x};
// Second Way
var query2 = from c in customers
from o in orders
where c.ID == o.ID && o.Product == "Shirt"
select new {c, o};
}
I found a lot of examples of First Way (using 'into'), and that is the way I used to do my LEFT OUTER JOINS. Recently I found the second way, and looks like it is simpler. I tested and both of them produce the same result. Now my question are there any hidden differences, performance, or it is only syntactic sugar?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不使用 !=null 检查,则应使用第一种方法
左外连接从左表中选择所有与右表中匹配的项目,如果右表中不存在匹配项,结果仍然是返回但右表中的值为 null(linq 将其转换为集合项类型的默认值)
您的两个查询都有效地执行了内部联接,因此它们将是相同的。
为了得到不同的结果
The First way should be used if you don't use the !=null check
A left outer join selects everything from the left table with all the items that match in the right table, if no matches exist in the right table the result is still returned but null for the values in the right table (linq translates this as the default value for the collection item type)
Both your queries effectively perform an inner join so they will be the same.
To get different results