在LINQ中使用未决请求时保存在内存中的信息[英] Information saved in memory when using pending request in LINQ

本文是小编为大家收集整理的关于在LINQ中使用未决请求时保存在内存中的信息的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我学习linq,我想使用未决的请求,但是我有这个问题

            List<string> _strs = new List<string> { "1", "2", "1", "1", "0" };
            var selind = _strs.Select((name, ind) => new { indexname = name, index = ind }).Where(o => o.indexname == "1");
            string sind = "";
            foreach (var item in selind)
                sind += item.index.ToString() + " ";
            //i get 0 2 3 
            _strs.Add("2");
            _strs.Add("1");
            sind = "";
            foreach (var item in selind)
                sind += item.index.ToString() + " ";
            //Good, i get 0 2 3 6
            _strs = new List<string>() { "1" };
            sind = "";
            foreach (var item in selind)
                sind += item.index.ToString() + " ";
            //Why i get again 0 2 3 6

好吧,我明白为什么,但是我想知道两件事:

  • 我应该如何清除内存?

    selind = null; 或者您可以告诉我更多的好方法?

  • 为与Selind合作,在_strs的总重建后,我找到了两种方式

            _strs.Clear();
            _strs.Add();
    

或再次致电

selind = _strs.Select((name, ind) => new { indexname = name, index = ind }).Where(o => o.indexname == "1");

你能以另一种方式建议我吗?

预先感谢!

推荐答案

您的查询:

_strs.Select((name, ind) => new { indexname = name, index = ind }).Where(o => o.indexname == "1");

与内存中的特定列表参考有关(当时_strs是什么),而不是特定变量名称.它们不是同一件事.当您这样做时:

_strs = new List<string>() { "1" };

您没有清除_strs最初指向的位置的内存参考.相反,您将该变量名称指向新的内存位置.而_strs.Clear()确实清除了原始列表.

解决问题的最佳解决方案是将LINQ查询包装在接受列表的函数中,因此您可以在新列表上再次调用它,而无需再次键入.另外,根据您的用例,只需在需要重新开始时致电.Clear()

(如果这不清楚,_strs = null对用来指向的列表没有任何作用,它只是使该特定变量名称无效)

本文地址:https://www.itbaoku.cn/post/1556994.html

问题描述

I learning LINQ, and i want to use pending request, but i have this problem

            List<string> _strs = new List<string> { "1", "2", "1", "1", "0" };
            var selind = _strs.Select((name, ind) => new { indexname = name, index = ind }).Where(o => o.indexname == "1");
            string sind = "";
            foreach (var item in selind)
                sind += item.index.ToString() + " ";
            //i get 0 2 3 
            _strs.Add("2");
            _strs.Add("1");
            sind = "";
            foreach (var item in selind)
                sind += item.index.ToString() + " ";
            //Good, i get 0 2 3 6
            _strs = new List<string>() { "1" };
            sind = "";
            foreach (var item in selind)
                sind += item.index.ToString() + " ";
            //Why i get again 0 2 3 6

Ok i understand why, but I wolud like to know two things:

  • How should i clear memory?

    selind = null; Or you can tell me more nice way?

  • For work with selind after total rebuild of _strs i find two way

            _strs.Clear();
            _strs.Add();
    

or call again

selind = _strs.Select((name, ind) => new { indexname = name, index = ind }).Where(o => o.indexname == "1");

Can you advice me another way?

Thanks in advance!

推荐答案

Your query:

_strs.Select((name, ind) => new { indexname = name, index = ind }).Where(o => o.indexname == "1");

Is tied to a particular list reference in memory (whatever _strs is at that time), not to a particular variable name. They are not the same thing. When you do this:

_strs = new List<string>() { "1" };

You're not clearing the memory reference of where _strs originally pointed. You are instead having that variable name point to a new memory location. Whereas _strs.Clear() does clear the original list.

The best solution for your problem is to wrap the LINQ query in a function that accepts a list, so you can call it again on new lists without typing it again. Alternatively, depending on your use case, just call .Clear() when you need to start again.

(in case this wasn't clear, _strs = null does nothing to the list that _strs used to point to, it just makes that particular variable name invalid)