在返回方法调用之前更新本地iqueryable列表 [英] Updating local iqueryable list before returning to method call

查看:187
本文介绍了在返回方法调用之前更新本地iqueryable列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对我的原始帖子的后续问题中,在iQueryable列表中使用Foreach如果在第二列表中找到值我找不到一个解决方案,当我使用foreach循环更新本地列表。通过我的视觉工作室调试器,它更新item.LinkURL值,如果当前项目在LookForMe列表中找到。但是当将Results.ToList()返回到我的调用方法时,此更新不在返回的集合中。如何在返回之前将它们保存到结果列表中?没有看到任何intellisense的东西,像Results.Update()或Results.Save()将工作。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆