I have to collections:
IEnumerable<lineResult> diplayedBondsList
and
List<string> pIsinList
lineResult is a very simple class defined as:
public class lineResult { public string isin { get ; set ; } public double rate { get ; set ; } public string issuer { get ; set ; } }
I am trying to create a new List with the string that are in pIsinList, but only if they are not already in the field isin of a "lineResult" element of diplayedBondsList. Kind of a "left XOR" (left because only one of the two lists elements would be added without a correspondance in the other table).
I am trying to not use too many loop because my lists have a very large amount of data, and I think that would slow down the program.
I have written this, but it does not seem to work, newBondLines always being empty:
IEnumerable<lineResult> newBondLines = diplayedBondsList.Where(item => pIsinList.IndexOf(item.isin) < 0);
foreach (lineResult lr in newBondLines)
{
newIsinList.Add(lr.isin);
}
In addition, I do use a loop, and maybe I could avoid it with a nice LINQ statement.
How could I 1) make this "left XOR" work and 2) improve its speed?
Using the
Enumerable.Except
:Note that this command will do implicitly a
Distinct()
onpIsinList
(something that isn't explained in the MSDN, but that if you look at the source is quite clear), so if you hadnew[] { "A", "A" }
inpIsinList
, the end result will be a single"A"
.You can do the
Except
"manually" to solve this "problem" (if it is a problem):