与使用Linq的另一个集合的一个集合的区别

时间:2016-06-05 14:08:52

标签: c# linq

我有两个系列。第一个是复杂类型,包含两个字符串属性

chpt_cd和appl_src_cd

src/main/resources

它存储在变量_validChapterCodeLst。

它的样本数据可能如下:

public class ChapterCodeValidationOutput
{
    public string chpt_cd { get; set; }
    public string appl_src_cd { get; set; }
}

生成集合的方法的输入是字符串的集合。

chpt_cd    aapl_src_cd
-------    -----------

07038      C062
06206      C191

可能包含以下数据:

List<string> _chapterCodes 

我想找到两个集合之间的区别,并将它们放在两个单独的列表中。

_validChapterCodeLst 中的任何一个都应该是有效输出列表,它应该同样有两列

chpt_cd及相关的appl_src_cd 无效列表应包含 _validChapterCodeLst _chapterCodes 输入列表之间的差异。并且同样应该包含两列。

我试过

'070038'

我尝试先将_validChapterCodeLst转换为List,然后执行Except。

但那没用。

此外,我不知道如何获取相关的appl_src_cd。

输出应为

06206 C191

2 个答案:

答案 0 :(得分:2)

Except仅接受相同类型的集合。但是,你可以尝试这个(我在这里使用HashSet以获得更好的性能):

var _chapterCodesHashSet = new HashSet<string>(_chapterCodes);
var _invalidChapterCodes = _validChapterCodeLst.Where(item => !_chapterCodesHashSet.Contains(item.chpt_cd)).ToList();

答案 1 :(得分:1)

我用的地方

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<ChapterCodeValidationOutput> _validChapterCodeLst = new List<ChapterCodeValidationOutput>() {
                new ChapterCodeValidationOutput() { chpt_cd =  "07038", appl_src_cd = "C062"},
                new ChapterCodeValidationOutput() { chpt_cd =  "06206", appl_src_cd = "C191"}
            };
            List<string> _chapterCodes = new List<string>() { "07038" };
            var results = _validChapterCodeLst.Where(x => !_chapterCodes.Contains(x.chpt_cd)).Select(y => new { chpt_cd = y.chpt_cd, appl_src_cd = y.appl_src_cd}).ToList();
        }
    }
    public class ChapterCodeValidationOutput
    {
        public string chpt_cd { get; set; }
        public string appl_src_cd { get; set; }
    }
}
相关问题