I have two collections. First one is of complex type that contains two string properties
chpt_cd and appl_src_cd
public class ChapterCodeValidationOutput
{
public string chpt_cd { get; set; }
public string appl_src_cd { get; set; }
}
And it's get stored in a variable _validChapterCodeLst.
It's sample data may look like :
chpt_cd aapl_src_cd
------- -----------
07038 C062
06206 C191
The input to the method that produces the collection is a collection of strings.
List<string> _chapterCodes
that may contain data like :
'070038'
I want to find the difference between the two collection and put them in two separate lists accordingly.
Whichever is there in the _validChapterCodeLst should be the Valid Output List and it should have two columns likewise
chpt_cd and associated appl_src_cd and the invalid list should contain the difference between _validChapterCodeLst and _chapterCodes input list. And also should contain two columns likewise.
I tried
gmvo._invalidChapterCodes = gmvi._chapterCodes.Except(_validChapterCodeLst.ConvertAll(x => x.chpt_cd.ToString())).ToList();
I tried to convert _validChapterCodeLst to List first and then perform Except.
But that did not work .
Also I do not know how to get the associated appl_src_cd .
Output should be
06206 C191
Except
accepts only collections of the same type. However, you can try this (I used HashSet
here for better performance):
var _chapterCodesHashSet = new HashSet<string>(_chapterCodes);
var _invalidChapterCodes = _validChapterCodeLst.Where(item => !_chapterCodesHashSet.Contains(item.chpt_cd)).ToList();
I used where
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; }
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.