新建了一个数据库,测试数据库正常,在项目中添加ADO.NET实体数据模型,关联这个数据库
我在用linq to xml把查询结果转换成xml,程序运行时报错
class Program
{
static void Main(string[] args)
{
using (var db = new BookContext())
{
var query = from store in db.Stores
orderby store.Name
select store;
foreach (var s in query)
{
XElement storeElement = new XElement
("store",
new XAttribute("name", s.Name),
new XAttribute("address", s.Address),
from stock in s.Stocks
select new XElement
("stock",
new XAttribute("StockID", stock.StockId),
new XAttribute("onHand", stock.OnHand),
new XAttribute("onOrder", stock.OnOrder),
new XElement
("book",
new XAttribute("title", stock.Book.Title),
new XAttribute("author", stock.Book.Author)
)
)
);
WriteLine(storeElement);
}
Write("Program finished, press Enter/Return to continue:");
ReadLine();
}
}
}
我一个一个XElement删的时候发现,报错的位置出现在这里,报错内容为 System.ArgumentNullException:“值不能为 null。
from stock in s.Stocks
select new XElement
但数据库中是有值的,为什么回报错呢