在返回方法调用之前更新本地iqueryable列表 [英] Updating local iqueryable list before returning to method call
问题描述
In a follow-up question to my original post Using Foreach on iQueryable List find value if in 2nd List I am unable to find a solution where when I make a change using the foreach loop it updates the local list. Walking through my visual studio debugger it does update the item.LinkURL value if the current item is found in the LookForMe list. But when returning the Results.ToList() back to my calling method this update is not in the returned collection. How do I save these into the Results list before returning them? Not seeing any intellisense where something like Results.Update() or Results.Save() would work.
foreach (var item in Results)
{
if (LookForME.Any(fs => item.LinkUrl.Contains(fs)))
{
item.LinkUrl = ServerPath + "/" + item.LinkUrl;
// works great until its time to return the updated Results list
}
// something here to update results with new value?
}
return Results.ToList();
推荐答案
您需要 >使用 ToList
并修改内存中的列表并返回。
You need to iterate first using ToList
and the modify the in-memory list and return that.
目前您的结果
是 IQueryable
更像是一个查询),但尚未执行。
Currently your Result
is IQueryable
(more like a query) which has yet to be executed.
您可以这样做:
var newList = Result.ToList(); //get a in-memory list
foreach (var item in newList) //modify in-memory list
{
if (LookForME.Any(fs => item.LinkUrl.Contains(fs)))
{
item.LinkUrl = ServerPath + "/" + item.LinkUrl;
// works great until its time to return the updated Results list
}
// something here to update results with new value?
}
return newList;
这篇关于在返回方法调用之前更新本地iqueryable列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!