myListingSyncIDs.AddRange(myListingSync.Listings);
#region Remove Listing References Fetched In The Last 24Hrs
// Listing References Fetched In The Last 24Hrs
// These will be excluded to optimise the running of the App.
// Basically meaning that a complete sync of all listings
// will only be done once every 24hrs
// So that if this is run every hr, it will not slow down the most recent additions
List<String> lstListingsUpdatedIn24Hrs = DAL.PropertyPortalDAL.GetSahtWebserviceUpdatesIn24Hrs();
List<P24SyncService.ListingSyncItem> myListingsUpdatedIn24Hrs =
lstListingsUpdatedIn24Hrs.Select(p => new P24SyncService.ListingSyncItem()
{
ListingNumber = p,
Status = P24SyncService.ListingState.AddedModified
}).ToList();
foreach (P24SyncService.ListingSyncItem myLSI in myListingsUpdatedIn24Hrs)
{
myListingSyncIDs.Remove(myLSI);
}
myListingSyncIDs.RemoveAll(p => lstListingsUpdatedIn24Hrs.Contains(p.ListingNumber));
#endregion
public partial class ListingSyncItem {
private string listingNumberField;
private ListingState statusField;
/// <remarks/> public string ListingNumber { get { return this.listingNumberField; } set { this.listingNumberField = value; } }
/// <remarks/> public ListingState Status { get { return this.statusField; } set { this.statusField = value; } } }
desi_develop...
Member
8 Points
10 Posts
List<Type> Remove
Feb 29, 2012 08:31 AM|LINK
Somebody explain to me this:
I am trying to delete items from a list with matching ids contained in another list of strings.
Step 1 is as below:
I'm trying to delete Items from myListingSyncIDs where the ListingNumber matches ListingNumbers in lstListingsUpdatedIn24Hrs.
The item at [0] Equals a value from lstListingsUpdatedIn24Hrs, as shown in Step 2:
But as shown in Step3: The Remove fails:
Then After doing a RemoveAll(func) Step4: The Remove works
Somebody explain why the Remove(item) doesn't work, Please ...
Reference: http://stackoverflow.com/questions/9496474/listtype-remove
Nazm
Participant
1760 Points
330 Posts
Re: List<Type> Remove
Feb 29, 2012 08:55 AM|LINK
Can you post your code?
desi_develop...
Member
8 Points
10 Posts
Re: List<Type> Remove
Feb 29, 2012 09:01 AM|LINK
myListingSyncIDs.AddRange(myListingSync.Listings); #region Remove Listing References Fetched In The Last 24Hrs // Listing References Fetched In The Last 24Hrs // These will be excluded to optimise the running of the App. // Basically meaning that a complete sync of all listings // will only be done once every 24hrs // So that if this is run every hr, it will not slow down the most recent additions List<String> lstListingsUpdatedIn24Hrs = DAL.PropertyPortalDAL.GetSahtWebserviceUpdatesIn24Hrs(); List<P24SyncService.ListingSyncItem> myListingsUpdatedIn24Hrs = lstListingsUpdatedIn24Hrs.Select(p => new P24SyncService.ListingSyncItem() { ListingNumber = p, Status = P24SyncService.ListingState.AddedModified }).ToList(); foreach (P24SyncService.ListingSyncItem myLSI in myListingsUpdatedIn24Hrs) { myListingSyncIDs.Remove(myLSI); } myListingSyncIDs.RemoveAll(p => lstListingsUpdatedIn24Hrs.Contains(p.ListingNumber)); #endregion
public partial class ListingSyncItem {
private string listingNumberField;
private ListingState statusField;
/// <remarks/>
public string ListingNumber {
get {
return this.listingNumberField;
}
set {
this.listingNumberField = value;
}
}
/// <remarks/>
public ListingState Status {
get {
return this.statusField;
}
set {
this.statusField = value;
}
}
}
mameenkhn
Contributor
2026 Points
391 Posts
Re: List<Type> Remove
Feb 29, 2012 09:04 AM|LINK
This is because you are removing an item which does not exists in myListingSyncIDs
foreach (P24SyncService.ListingSyncItem LSI24 in myListingsUpdatedIn24Hrs) { foreach (P24SyncService.ListingSyncItem myLSI in myListingSyncIDs) { if(myLSI.ListingNumber == LSI24.ListingNumber) { myListingSyncIDs.Remove(myLSI); break; } } }--------------------------------------------------
Muhammad Amin
محمد امين
desi_develop...
Member
8 Points
10 Posts
Re: List<Type> Remove
Feb 29, 2012 09:07 AM|LINK
That would work, so as myListingSyncIDs.RemoveAll(delegate);
I'm just wondering why myListingSyncIDs.Remove(myLSI);
doesnt work ...
mameenkhn
Contributor
2026 Points
391 Posts
Re: List<Type> Remove
Feb 29, 2012 09:09 AM|LINK
myLSI reference is not present in myListingSyncIDs
it is in myListingsUpdatedIn24Hrs
--------------------------------------------------
Muhammad Amin
محمد امين
Nazm
Participant
1760 Points
330 Posts
Re: List<Type> Remove
Feb 29, 2012 11:03 AM|LINK
Try to never remove anything from a collection you're iterating over while you're inside a loop. except if you're using while or
iterate through the list backwards..
for (int x = yourList.Count - 1; x >= 0; x--) { if ("عمل صالح") yourList.RemoveAt(x); }desi_develop...
Member
8 Points
10 Posts
Re: List<Type> Remove
Feb 29, 2012 11:06 AM|LINK
I wasnt trying to, I'm deleting from List1 when a reference matches any value in List2.
desi_develop...
Member
8 Points
10 Posts
Re: List<Type> Remove
Feb 29, 2012 11:09 AM|LINK
A value in myListingsUpdatedIn24Hrs was equal to myLS. So , Yes it is.
But the explanation that I got was:
<div class="post-text">The
type doesn't override , so doesn't know that the item to remove is "equal" to the item in your list.Simply overriding
and appropriately (to check for the equality of and , presumably, and build a hash code based on those) should fix the problem.For
, you're providing a predicate. That doesn't use , which is why it's working. </div>mameenkhn
Contributor
2026 Points
391 Posts
Re: List<Type> Remove
Feb 29, 2012 11:19 AM|LINK
You have to understand the difference between Value Type and Reference Type....
--------------------------------------------------
Muhammad Amin
محمد امين