1

I have a Customer with a list of Contacts. This list is an ISet collection. I can't do a Linq query on it. Could you help me for this ?

Thanks,

public class Customer
{
    public virtual Iesi.Collections.Generic.ISet<Contact> Contacts { get; set; }
}

Customer customer = session.Get(id);
customer.Contacts = // Error - customer.Contacts.Where(x => x.Id != contactId);

Update 1

Tried this : from p in customer.Contacts.AsEnumerable() where p.Id != id select p; error on the where.System.Collections.Generic.IEnumerable' to 'Iesi.Collections.Generic.ISet'. An explicit conversion exists (are you missing a cast?)

iandotkelly
  • 9,024
  • 8
  • 48
  • 67
TheBoubou
  • 19,487
  • 54
  • 148
  • 236

2 Answers2

4

I believe this problem has nothing to do with IESI ISet<T> implementing IEnumerable<T> (it does, BTW), but the answer is instead pointed to by the cast exception mentioned in the 'update' to the original post.

The line...

customer.Contacts = customer.Contacts.Where(x => x.Id != contactId);

...does in fact (incorrectly) attempt to assign an IEnumerable<Contact> (the result of the .Where(...) operator) to a property of type ISet<Contact> (the .Contacts property on the Customer class).

I strongly suspect that this line would be fine...

IEnumerable<Contact> contacts = customer.Contacts.Where(x => x.Id != contactId);

...demonstrating that the .Where(...) operator works just fine on the IESI ISet<T> but that what .Where(...) returns is (of course) IEnumerable<T>.

For this to work, you need to convert the result of your .Where(...) operation from IEnumerable<T> to ISet<T> before attempting to assign it to the customer.Contacts property.

sbohlen
  • 1,999
  • 1
  • 15
  • 18
0

I'm assuming ISet is System.Collections.Generic.ISet<T>.

Add a using statement for System.Linq, and possibly a reference to System.Core.dll.


If it's something else as the root namespace Iesi might indicate, can you use the standard ISet<T>? Or, can you somehow convert your ISet<T> to an IEnumerable<T>?

George Duckett
  • 31,770
  • 9
  • 95
  • 162