【问题标题】:How to use Where clause in a ICollection [duplicate]如何在 ICollection 中使用 Where 子句
【发布时间】:2017-01-30 16:28:21
【问题描述】:

您好,我有一个 ICollection:

using System.Linq;
...
public virtual ICollection<MyObject> MyList { get; set; }

并且想要获取第一个结果或者使用Where子句在实现的属性中查找结果,例如:

var x = test.MyList.Where(c => c.MyId == 1);

但我的对象没有列出 Where 或 FirstOrDefault 等方法

【问题讨论】:

  • 使用.First(c =&gt; c.MyId == 1).FirstOrDefault(c =&gt; c.MyId == 1) 并关闭问题。延伸阅读tutorialspoint.com/linq
  • 为了改进未来的问题,请仔细阅读minimal reproducible example发布代码指南。
  • 我使用 System.Linq 但不起作用,为什么?
  • 为什么您不能提供演示问题的示例和确切的错误消息?其他人无法知道您做错了什么,导致看似正确的代码在编译时失败。

标签: c# .net generics


【解决方案1】:

应该这样做:

var x = test.MyList.Where(c => c.MyId == 1).FirstOrDefault();

【讨论】:

  • 我试过了,但它不起作用或列出了方法。我的对象是一个 ICollection
  • 尝试使用 System.Linq 添加;到 using 块(我猜你的意思是 Visual Studio IntelliSense 没有列出方法?)
  • 我使用 System.Linq 但不起作用,为什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 2020-11-30
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多