Where Not In OR Except simulation of SQL in LINQ to Objects
- by Thinking
Suppose I have two lists that holds the list of source file names and destination file names respectively.
The Sourcefilenamelist has files as 1.txt, 2.txt,3.txt, 4.txt
while the Destinaitonlist has 1.txt,2.txt.
I ned to write a linq query to find out which files are in SourceList that are absent in DestinationFile list.
e.g. here the out put will be 3.txt and 4.txt.
I have done this by a foreach statement. but now I want to do the same by using LINQ(C#).
Edit:
My Code is
List<FileList> sourceFileNames = new List<FileList>();
sourceFileNames.Add(new FileList { fileNames = "1.txt" });
sourceFileNames.Add(new FileList { fileNames = "2.txt" });
sourceFileNames.Add(new FileList { fileNames = "3.txt" });
sourceFileNames.Add(new FileList { fileNames = "4.txt" });
List<FileList> destinationFileNames = new List<FileList>();
destinationFileNames.Add(new FileList { fileNames = "1.txt" });
destinationFileNames.Add(new FileList { fileNames = "2.txt" });
IEnumerable<FileList> except = sourceFileNames.Except(destinationFileNames);
And Filelist is a simple class with only one property fileNames of type string.