IQueryable从集合中删除,最好的方法?(IQueryable remove from the collection, best way?)

编程入门 行业动态 更新时间:2024-10-27 00:26:35
IQueryable从集合中删除,最好的方法?(IQueryable remove from the collection, best way?) IQueryable<SomeType> collection = GetCollection(); foreach (var c in collection) { //do some complex checking that can't be embedded in a query //based on results from prev line we want to discard the 'c' object } //here I only want the results of collection - the discarded objects

所以用这个简单的代码,得到结果的最好方法是什么。 我应该在foreach之前创建一个List并插入我想保留的对象,或者有其他方式可以更好地完成这种类型的事情。

我知道有类似主题的其他帖子,但我只是不觉得我得到了我需要的东西。

编辑我试过了

var collection = GetCollection().Where(s => { if (s.property == 1) { int num= Number(s); double avg = Avg(s.x); if (num > avg) return true; else return false; } else return false; });

我试过这个,但是在编译时被赋予了“带声明体的lambda表达式不能转换为表达式树”。 我没有做对吗?

IQueryable<SomeType> collection = GetCollection(); foreach (var c in collection) { //do some complex checking that can't be embedded in a query //based on results from prev line we want to discard the 'c' object } //here I only want the results of collection - the discarded objects

So with that simple code what is the best way to get the results. Should I created a List just before the foreach and insert the objects I want to keep, or is there some other way that would be better to do this type of thing.

I know there are other posts on similar topics but I just don't feel I'm getting what I need out of them.

Edit I tried this

var collection = GetCollection().Where(s => { if (s.property == 1) { int num= Number(s); double avg = Avg(s.x); if (num > avg) return true; else return false; } else return false; });

I tried this but was given "A lambda expression with a statement body cannot be converted to an expression tree" on compile. Did I not do something right?

最满意答案

//do some complex checking that can't be embedded in a query

我不明白。 你可以传递一个委托,它可以指向一个非常复杂的函数(图灵完成),检查你是否应该放弃它:

var result = GetCollection().AsEnumerable().Where(c => { // ... // process "c" // return true if you want it in the collection });

如果你愿意,你可以在另一个函数中重构它:

var result = GetCollection.Where(FunctionThatChecksToDiscardOrNot);
//do some complex checking that can't be embedded in a query

I don't get it. You can pass a delegate which can point to a very complex function (Turing-complete) that checks whether you should discard it or not:

var result = GetCollection().AsEnumerable().Where(c => { // ... // process "c" // return true if you want it in the collection });

If you want, you can refactor it in another function:

var result = GetCollection.Where(FunctionThatChecksToDiscardOrNot);

更多推荐

本文发布于:2023-08-03 12:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1389381.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最好的   方法   IQueryable   remove   collection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!