使用Linq从其他两个列表中填充单个列表[英] Use LINQ to populate a single list from two other lists

本文是小编为大家收集整理的关于使用Linq从其他两个列表中填充单个列表的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

现在,我有2个列表(_pumponespm和_pumptWospm)是数据库查询的结果.现在,我一直在使用LINQ填充列表供我使用以进行计算.类似的东西:

var spmOne = _pumpOneSpm.Where((t, i) => _date[i].Equals(date) &&
            DateTime.Compare(_time[i], DateTime.Parse(start)) > 0 && 
            DateTime.Compare(_time[i], DateTime.Parse(end)) < 0).ToList();

这很好,因为我一直从一个列表中提取数据将特定数据添加到另一个列表中.但是现在,我需要从两个列表中提取数据才能添加到一个列表中.我很好奇是否有一种方法可以使用LINQ进行操作,或者我应该只用for loop迭代?这是我使用的循环:

        for (var i = 0; i < _pumpOneSpm.Count; i++)
        {
            if (_date[i].Equals(date))
            {
                if (DateTime.Compare(_time[i], DateTime.Parse(start)) > 0)
                {
                    if (DateTime.Compare(_time[i], DateTime.Parse(end)) < 0)
                    {
                        spmOne.Add(_pumpOneSpm[i]);
                        spmOne.Add(_pumpTwoSpm[i]);
                    }
                }
            }
        }

这有效,并做我想要的.但是我只是想让它更加高效,更快.我可以为每个列表使用类似于第一个代码块的两行,但这意味着我要迭代两次.因此,要重申我的问题,是否可以使用linq命令同时从两个列表中获取数据来填充另一个列表,或者我应该坚持使用循环吗?感谢您的所有帮助.

编辑

我没有提及,使用这些步骤的函数的目的是罚款_pumponespm&_pumptWospm的最大值.不是每个的最大值,而是它们之间的最大值.因此,最初我将它们添加到一个列表中,只是调用spmone.max().但是,有了上面的两个LINQ查询,我正在运行一个IF语句以比较两个最大值,然后返回两者中的大部分.因此,从技术上讲,他们不需要在一个列表中,我只是认为如果它们是的话会更容易处理.

推荐答案

因此,最简单的方法就是这样:

var resultList = list1.Concat(list2).ToList();

请注意,这将与您的循环略有不同(项目的顺序不会相同).它将从一个列表中提供所有项目,然后是另一个项目,而不是将第一个项目,第二个,第二个项目中的第一个,等等.方法调用.

您已经提到了性能;值得注意的是,您不会比for循环变得更好,性能.您可以获得更短的代码;代码更可读性,并且将执行有关相同的执行.在运行时速度方面,根本没有进步的空间.

也值得注意的是,您可以将这两种方法结合在一起;这甚至不是一个坏主意.您可以使用LINQ过滤两个输入序列(使用Where),然后在结果上foreach将它们添加到列表中(或使用AddRange).

var query1 = _pumpOneSpm.Where((t, i) => _date[i].Equals(date) &&
            DateTime.Compare(_time[i], DateTime.Parse(start)) > 0 && 
            DateTime.Compare(_time[i], DateTime.Parse(end)) < 0);
var query2 = ...
List<T> results = new List<T>(query1);
results.AddRange(query2);

其他推荐答案

因此,要重申我的问题,是否可以使用linq命令来 同时从两个列表中获取数据以填充另一个列表,或 我应该坚持循环吗?

合并列表:

var mergedList = list1.Union(list2).ToList();

如果需要,请进行过滤:

var filtered = mergedList.Where( p => ... filter ... );

和最大:

var max = filtered.Max();

注意:

正如OP所说,不需要订单的不需要命令,因此工会可以.

其他推荐答案

您可以使用SelectMany方法

BB548748.aspx

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

问题描述

Right now, I have 2 lists(_pumpOneSpm and _pumpTwoSpm) that are the result of a database query. Now, I've been using LINQ to populate lists for me to work with for calculations. Something similar to this:

var spmOne = _pumpOneSpm.Where((t, i) => _date[i].Equals(date) &&
            DateTime.Compare(_time[i], DateTime.Parse(start)) > 0 && 
            DateTime.Compare(_time[i], DateTime.Parse(end)) < 0).ToList();

This has been fine, because I've been pulling data from one list to add specific data into another list. But now, I need pull data from two lists to add to one list. I'm curious if there is a way to do this with LINQ, or if I should just iterate through with a for loop? Here's what I've got with the for loop:

        for (var i = 0; i < _pumpOneSpm.Count; i++)
        {
            if (_date[i].Equals(date))
            {
                if (DateTime.Compare(_time[i], DateTime.Parse(start)) > 0)
                {
                    if (DateTime.Compare(_time[i], DateTime.Parse(end)) < 0)
                    {
                        spmOne.Add(_pumpOneSpm[i]);
                        spmOne.Add(_pumpTwoSpm[i]);
                    }
                }
            }
        }

This works, and does what I want. But I'm just trying to make it a bit more efficient and faster. I could use two lines similar to the first code block for each list, but that means I'm iterating through twice. So, to reiterate my question, is it possible to use a LINQ command to pull data from two lists at the same time to populate another list, or should I stick with my for loop? Thanks for any and all help.

EDIT

I failed to mention, that the purpose of the function utilizing these steps is to fine the MAX of _pumpOneSpm & _pumpTwoSpm. Not the max of each, but the max between them. Thus initially I was adding them into a single list and just calling spmOne.Max(). But with two LINQ queries like above, I'm running an if statement to compare the two maxes, and then return the greater of the two. So, technically they don't NEED to be in one list, I just thought it'd be easier to handle if they were.

推荐答案

So the simplest way to do this would be something like this:

var resultList = list1.Concat(list2).ToList();

Note that this will be slightly different than your for loop (the order of the items won't be the same). It will have all items from one list followed by all items from another, rather than having the first of the first, the first of the second, etc. If that's important you could still do it with LINQ, but it would take a few extra method calls.

You've mentioned performance; it's worth noting that you aren't going to get significantly better, performance wise, than your for loop. You could get code that's shorter; code that's more readable, and that will perform about the same. There simply isn't room to improve very much more though in terms of runtime speed.

It's also worth noting that you can combine the two method; it's not even a bad idea. You can use LINQ to filter the two input sequences (using Where) and then foreach over the results to add them to the list (or use AddRange).

var query1 = _pumpOneSpm.Where((t, i) => _date[i].Equals(date) &&
            DateTime.Compare(_time[i], DateTime.Parse(start)) > 0 && 
            DateTime.Compare(_time[i], DateTime.Parse(end)) < 0);
var query2 = ...
List<T> results = new List<T>(query1);
results.AddRange(query2);

其他推荐答案

So, to reiterate my question, is it possible to use a LINQ command to pull data from two lists at the same time to populate another list, or should I stick with my for loop?

Merge the lists:

var mergedList = list1.Union(list2).ToList();

Optionally if you want, then filter:

var filtered = mergedList.Where( p => ... filter ... );

And for the Max:

var max = filtered.Max();

NOTE:

As OP says there's no need from order not for not-duplicates so Union will be OK.

其他推荐答案

You can use the SelectMany method

http://msdn.microsoft.com/en-us/library/bb548748.aspx

相关标签/搜索