【问题标题】:Linq query on ISet collectionISet 集合上的 Linq 查询
【发布时间】:2011-08-23 22:11:31
【问题描述】:

我有一个带有联系人列表的客户。此列表是一个 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);

更新 1

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

【问题讨论】:

    标签: c# linq nhibernate


    【解决方案1】:

    我相信这个问题与 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 属性。

    【讨论】:

      【解决方案2】:

      我假设 ISet 是System.Collections.Generic.ISet&lt;T&gt;

      添加 System.Linq 的 using 语句,并可能引用 System.Core.dll


      如果它是根命名空间Iesi 可能指示的其他内容,您可以使用标准ISet&lt;T&gt; 吗?或者,您能否以某种方式将您的ISet&lt;T&gt; 转换为IEnumerable&lt;T&gt;

      【讨论】:

      • 不,它是来自 NHibernate 的 ISet 集合
      猜你喜欢
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      相关资源
      最近更新 更多