Skip to main content
 首页 » 编程设计

c#之对 ISet 集合的 Linq 查询

2024年02月23日32Free-Thinker

我有一个客户有一个联系人列表。这个列表是一个 ISet 集合。我无法对其进行 Linq 查询。你能帮我吗?

谢谢,

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); 

更新一

试过这个:from p in customer.Contacts.AsEnumerable() where p.Id != id select p; where.System.Collections.Generic.IEnumerable 错误 到“Iesi.Collections.Generic.ISet”。 存在显式转换(是否缺少强制转换?)

请您参考如下方法:

我相信这个问题与 IESI ISet 实现 IEnumerable 无关(顺便说一句,它 确实),但答案是由“更新”到原始帖子。

这条线...

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

...实际上(错误地)尝试将 IEnumerable (.Where(...) 运算符的结果)分配给 ISet 类型的属性(客户类别)。

我强烈怀疑这条线没问题......

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

...证明 .Where(...) 运算符在 IESI ISet 上运行良好,但 .Where(...) 返回的(当然)是 IEnumerable 。

为此,您需要将 .Where(...) 操作的结果从 IEnumerable 转换为 ISet ,然后再尝试将其分配给 customer.Contacts 属性。