我将从事一个需要使用Linq的 WinForms 项目,但我对此一无所知,所以我才开始学习。
在阅读了有关 Linq 的教程后,我使用Microsoft Windows SDK 附带的工具创建了System.Data.Linq.DataContext
该工具。SqlMetal
我用作示例的数据库是"Northwind"。
我已经能够进行如下简单的查询:
Northwind db = new Northwind("Data Source=local;Initial Catalog=Northwind;Integrated Security=True");
var query = from c in db.Customers
join o in db.Orders on c.CustomerID equals o.CustomerID
select new
{
Customer_ID = c.CustomerID,
Customer_Name = c.ContactName,
Order_ID = o.OrderID
};
然后我在 a 中显示结果,DataGridView
一切顺利。
但是现在我面临一个我无法解决的问题。
我试图修改以前的查询以制作 a LEFT OUTER JOIN
,所以我所做的如下:
var query = from c in db.Customers
join o in db.Orders on c.CustomerID equals o.CustomerID into j
from or in j.DefaultIfEmpty()
select new
{
Customer_ID = c.CustomerID,
Customer_Name = c.ContactName,
Order_ID = or.OrderID
};
但是,当我尝试在DataGridView
错误中显示这些数据集时:
The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.
我知道这个问题是由没有“订单”的“客户”引起的,因此系统会尝试为null
变量int
赋值。
但是,我怎样才能LEFT OUTER JOIN
没有这个错误?
先感谢您。