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

查看:63
本文介绍了使用LINQ从其他两个列表中填充一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我有2个列表(_pumpOneSpm和_pumpTwoSpm)是数据库查询的结果.现在,我一直在使用LINQ来填充列表,以便我进行计算.与此类似:

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();

很好,因为我一直在从一个列表中提取数据以将特定数据添加到另一个列表中.但是现在,我需要从两个列表中提取数据以添加到一个列表中.我很好奇是否可以用LINQ做到这一点,或者我是否应该仅通过for循环进行迭代?这是我在for循环中得到的东西:

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]);
                    }
                }
            }
        }

这有效,并且可以完成我想要的.但是我只是想使其变得更有效率和更快.对于每个列表,我可以使用两行类似于第一个代码块的代码,但这意味着我要遍历两次.因此,重申我的问题,是否可以使用LINQ命令从两个列表中同时提取数据以填充另一个列表,还是应该坚持使用for循环?感谢您提供的所有帮助.

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.

编辑

我没有提及,利用这些步骤的功能的目的是优化_pumpOneSpm&的最大值. _pumpTwoSpm.不是每个的最大值,而是它们之间的最大值.因此,最初我将它们添加到单个列表中,然后仅调用spmOne.Max().但是对于上面的两个LINQ查询,我正在运行一个if语句来比较两个最大值,然后返回两者中较大的一个.因此,从技术上讲,它们并不需要排在一个列表中,我只是认为如果它们在列表中会更容易处理.

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.

推荐答案

最简单的方法如下:

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

请注意,这与您的for循环略有不同(项目的顺序将不同).它将具有一个列表中的所有项目,然后是另一个列表中的所有项目,而不是具有第一个列表中的第一个,第二个列表中的第一个,依此类推.如果这很重要,您仍然可以使用LINQ进行操作,但是这需要一些额外的操作方法调用.

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.

您已经提到性能;值得注意的是,就性能而言,您不会比for循环好得多.您可以获得较短的代码;更具可读性的代码,并且将执行与大致相同的代码.尽管就运行速度而言,根本没有太多改进的余地.

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.

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

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);

这篇关于使用LINQ从其他两个列表中填充一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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