帮忙看看这个简单的LInQ怎么修改!
var query1 =
from obj in result
select new
{
obj.ID,
obj.Name,
objary = obj.GetObjary(),
keyObjary = (from o in objary
select new
{
o.ID,
parts = GetParts( o.ID)
}).ToArray()
};
private object GetParts( int id)
{
XXX
}
红色部分有错误,提示“当前上下文中不存在名称objary ,
我可以把obj.GetObjary(),就没有错误了,
可是这么做,导致多调用了obj.GetObjary(),
影响效率,
应该怎么修改呢?
------解决方案--------------------
试试这样:【let的使用】
var query1 =
from obj in result
let jary = obj.GetObjary()
select new
{
obj.ID,
obj.Name,
objary = jary ,
keyObjary = (from o in jary
select new
{
o.ID,
parts = GetParts( o.ID)
}).ToArray()
};