在 LinQ 中应用Where 和 Take 时出现问题

发布于 2024-10-26 05:48:19 字数 603 浏览 1 评论 0原文

我有一个 List,我想对其应用 Take 和Where 条件,但它不起作用,我想知道问题是什么。我的查询是

List<MediaRef> objmed = new List<MediaRef>();
objmed = GetRecords(); //GetRecords will fetch records
objmed.Where(e => e.Title.Contains(Keyword)) //This line is not working

Where条件不起作用,但是当我将其更改为

List<MediaRef> objmed = new List<MediaRef>();
objmed = GetRecords();
objmed = (from p in objmed 
         where p.Title.Contains(Keyword)
         select p).ToList(); 

它时工作正常。我使用 Take() 函数面临同样的问题。可能是什么问题?

I have a List<MediaRef> and i want to apply Take and Where condition on it but it is not working i wonder what is the issue. My query is

List<MediaRef> objmed = new List<MediaRef>();
objmed = GetRecords(); //GetRecords will fetch records
objmed.Where(e => e.Title.Contains(Keyword)) //This line is not working

and Where condition is not working but when i change this to

List<MediaRef> objmed = new List<MediaRef>();
objmed = GetRecords();
objmed = (from p in objmed 
         where p.Title.Contains(Keyword)
         select p).ToList(); 

It works fine. I am facing same problem using Take() function. What may be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅沫记忆 2024-11-02 05:48:19

objmed.Where(e => e.Title.Contains(Keyword)) //此行不起作用

你需要分配结果,Were运算符不会更改源集合,它创建一个惰性输出集合。

当您枚举 Where 的结果时,它将读取源集合并返回符合条件的元素。在您的工作示例中,您 (1) 分配结果,(2) 使用 ToList 强制枚举。

尝试:

// No need to create a List<> only to throw it away
List<MediaRef> objmed = GetRecords();
objmed = objmed.Where(e => e.Title.Contains(Keyword)).ToList();

objmed.Where(e => e.Title.Contains(Keyword)) //This line is not working

You need to assign the result, the Were operator doesn't change the source collection, it creates a lazy output collection.

When you enumerate the result of Where it will read the source collection and return the elements that match the condition. In your working example you are (1) assigning the result, and (2) using ToList to force enumeration.

Try:

// No need to create a List<> only to throw it away
List<MediaRef> objmed = GetRecords();
objmed = objmed.Where(e => e.Title.Contains(Keyword)).ToList();
半岛未凉 2024-11-02 05:48:19

试试这个:

List<MediaRef> objmed = new List<MediaRef>();
objmed = GetRecords(); //GetRecords will fetch records
objmed = objmed.Where(e => e.Title.Contains(Keyword));

您忘记将 where 的结果分配给某物。

Try this:

List<MediaRef> objmed = new List<MediaRef>();
objmed = GetRecords(); //GetRecords will fetch records
objmed = objmed.Where(e => e.Title.Contains(Keyword));

You forgot to assign the result of where to something.

拥醉 2024-11-02 05:48:19

试试这个...

var records = GetRecords().Where(e => e.Title.Contains(Keyword)).ToList();

如果您要使用 foreach 枚举列表,则不需要 ToList()

Try this...

var records = GetRecords().Where(e => e.Title.Contains(Keyword)).ToList();

The ToList() is unnecessary if you are going to enumerate the list with foreach.

〃安静 2024-11-02 05:48:19

您需要将 where 的输出分配给新变量 - 例如

var withWhere = objmed.Where(e => e.Title.Contains(Keyword));
var withTakeAndWhere = withWhere.Take(10);

,或者,当然您可以使用流体样式:

List<MediaRef> objmed = 
       GetRecords()
          .Where(e => e.Title.Contains(Keyword))
          .Take(10)
          .ToList();

You need to assign the output of where to a new variable - e.g.

var withWhere = objmed.Where(e => e.Title.Contains(Keyword));
var withTakeAndWhere = withWhere.Take(10);

or, of course you can use fluid styling:

List<MediaRef> objmed = 
       GetRecords()
          .Where(e => e.Title.Contains(Keyword))
          .Take(10)
          .ToList();
三月梨花 2024-11-02 05:48:19

你想达到什么目的? .Where 和 .Take 方法根据给定的条件返回对象列表。

代码应该是:

List<MediaRef> objmed = new List<MediaRef>();
objmed = GetRecords(); //GetRecords will fetch records
objmed = objmed.Where(e => e.Title.Contains(Keyword)) //This line is not working

What are you trying to achieve? The .Where and .Take methods return a list of objects depending on the criteria given.

The code should be:

List<MediaRef> objmed = new List<MediaRef>();
objmed = GetRecords(); //GetRecords will fetch records
objmed = objmed.Where(e => e.Title.Contains(Keyword)) //This line is not working
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文