所以用这个简单的代码,得到结果的最好方法是什么。 我应该在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 objectsSo 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);更多推荐
发布评论