Reputation: 409
i m using C# 4.0.
i want to remove some element from my list.
i have a list A(type String) and List B(type T).
i want to take all element that in List A but not in list B --> i save it in Temp_Od_Scan (it works)
and all element that in List B but not in list A --> i save it in Temp_Od_Donne (it not works)
this code works fine (take all element that in List A but not in list B):
bool Is_Egal = true;
foreach (string Od_Scan in Ordre_Scan)
{
foreach (Pers_Compare Od_Done in Outils.Get_Ordre_Donne())
{
if (Od_Scan == Od_Done.NoOrdre.ToString() && !String.IsNullOrWhiteSpace(Od_Done.NoOrdre))
{
Temp_Od_Scan_Egal.Add(Od_Scan);
}
else
{
Is_Egal = false;
}
}
}
Temp_Od_Scan = Ordre_Scan.Except(Temp_Od_Scan_Egal).ToList();
but this code not work (all element that in List B but not in list A), it not remove the element.
foreach (Pers_Compare Od_Done in Outils.Get_Ordre_Donne())
{
foreach (string Od_Scan in Ordre_Scan)
{
if (Od_Done.NoOrdre == Od_Scan)
{
Temp_Od_Donne_Egal.Add(Od_Done);
}
else
{
Is_Egal = false;
}
}
}
Temp_Od_Donne = Outils.Get_Ordre_Donne().Except(Temp_Od_Donne_Egal).ToList();
and then i ask my friend google, and found another solution that not work also:
Temp_Od_Donne = Outils.Get_Ordre_Donne();
foreach (Pers_Compare donneexist in Temp_Od_Donne_Egal) Temp_Od_Donne.Remove(donneexist);
or like this:
Temp_Od_Donne.RemoveAll(i => Temp_Od_Donne_Egal.Contains(i));
or like this:
var list1 = Outils.Get_Ordre_Donne().Where(o => Ordre_Scan.Any(s => s != o.NoOrdre));
my class look like this:
public class Pers_Compare
{
string _NoLV;
public string NoLV
{
get { return _NoLV; }
set { _NoLV = value; }
}
string _NoOrdre;
public string NoOrdre
{
get { return _NoOrdre; }
set { _NoOrdre = value; }
}
public int CompareTo(string other)
{
return this.NoOrdre.CompareTo(other);
}
}
there are no error, i just not remove the element. i have 345 element in A and 3 element in B. 1 element in A match 1 element in B so the list Temp_Od_Donne return 2 (correct) but the list Temp_Od_Donne return 345 (not correct, it should return 344)
but strange is about this loop:
Temp_Od_Donne = Outils.Get_Ordre_Donne();
foreach (Pers_Compare donneexist in Temp_Od_Donne_Egal)
{
Temp_Od_Donne.Remove(donneexist);
}
before it enter the loop Temp_Od_Donne.count = 345 and after it enter the loop Temp_Od_Donne.count = 345...strange...
Upvotes: 0
Views: 131
Reputation: 121
I hope this helps you out. I have used some linq queries to solve your problem.
List<string> Ordre_Scan = "aabb,eeff".Split(',').ToList(); // input for test (guess the scancodes)
List<string> pers_Compare_ScanCodes = "aabb,ccdd,eeff".Split(',').ToList(); // input for test (existing class string values)
List<Pers_Compare> pers_Compare = (from scan_code in pers_Compare_ScanCodes select
new Pers_Compare { NoOrdre = scan_code }).ToList(); // straight to a list to match your problem
var resContains = from pers in pers_Compare
where !string.IsNullOrWhiteSpace(pers.NoOrdre)
&&
Ordre_Scan.Contains(pers.NoOrdre.Trim())
select pers;
var resNotContains = pers_Compare.Except(resContains.ToList());
Upvotes: 1
Reputation: 448
Try this:
foreach (Pers_Compare Od_Done in Outils.Get_Ordre_Donne())
{
foreach (string Od_Scan in Ordre_Scan)
{
if (Od_Done.NoOrdre.ToString() == Od_Scan)
{
Temp_Od_Donne_Egal.Add(Od_Done);
}
else
{
Is_Egal = false;
}
}
}
Temp_Od_Donne = Outils.Get_Ordre_Donne().Except(Temp_Od_Donne_Egal).ToList();
Upvotes: 0
Reputation: 86
I'm not too sure I completely understand the question, but if you have managed to put all the elements that are in List A but not in List B into a separate list, then surely you could just switch your List A parts for List B and it will produce the same result?
Switch all of List A - List B Switch all of List B - List A Switch all of Temp_Od_Scan - Temp_Od_Donne
bool Is_Egal = true;
foreach (Pers_Compare Od_Done in Outils.Get_Ordre_Donne())
{
foreach (string Od_Scan in Ordre_Scan)
{
if (Od_Scan == Od_Done.NoOrdre.ToString() && !String.IsNullOrWhiteSpace(Od_Done.NoOrdre))
{
Temp_Od_Donne_Egal.Add(Od_Done);
}
else
{
Is_Egal = false;
}
}
}
Temp_Od_Donne = Outils.Get_Ordre_Donne().Except(Temp_Od_Donne_Egal).ToList();
This may not be correct or even close to the answer, I am quite new to programming and just thought I might try and help out, hope this helps.
Upvotes: 0