Linq - 有一个复杂的对象限制吗? [英] Linq - is there a complex object limit?
问题描述
我有一个对象如下:Inventory-> Item-> ItemName
其中Inventory是具有多个属性的库存对象。物品就是其中之一。它有几个属性,例如ItemName。
我的查询如下:
var displayInventory =来自库存中的inv选择inv.Item.ItemName;
当我绑定时,我得到一个空引用错误到我的数据网格。似乎Linq不能将超过2个级别下降到一个对象中。
我尝试过的事情:
string itemname = inventory.First()。Item.ItemName; //工作正常信息是那里的var displayInventory =来自库存中的inv选择inv.Item //当我绑定到我的网格时也能正常工作。
请告诉我这不是Linq限制。
-Scott
I have an object as follows: Inventory->Item->ItemName
where Inventory is an inventory object with several properties. Item is one of them. It has several properties, such as ItemName.
I have a query as follows:
var displayInventory = from inv in inventory select inv.Item.ItemName;
I get a null reference error when I bind to my datagrid. It seems like Linq can't descend more than 2 levels into an object.
Things I have tried:
string itemname = inventory.First().Item.ItemName; //that works fine. The information is there
var displayInventory = from inv in inventory select inv.Item //that works fine too when I bind to my grid.
Please tell me this isn't a Linq limitation.
-Scott
推荐答案
限制是Linq只能应用于实现IEnumerable< T>的对象。看起来像ItemName不是。
A limitation is that Linq can only applied to the object implemented IEnumerable<T>. Looks like ItemName is not.
这篇关于Linq - 有一个复杂的对象限制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!