lambda

Can a class inherit from LambdaExpression in .NET? Or is this not recommended?

Consider the following code (C# 4.0): public class Foo : LambdaExpression { } This throws the following design-time error: Foo does not implement inherited abstract member System.Linq.Expressions.LambdaExpression.Accept(System.Linq.Expressions.Compiler.StackSpiller) There's absolutely no problem with public class Foo : Expression { }...

VB.Net & type inferencing in lamda expressions?

I have a type lets call it "MyType". I have a List(Of MyType). Here is what i'm doing: MyList.Sum(Function(x) x.MyFieldToTotal) "MyFieldToTotal" is a decimal. For the life of me i can't figure out why x above is an object rather than a type of "MyType". Shouldn't Type Inferencing be working in this case? Even in intellisense i get "...

Get Method Name Using Lambda Expression

I'm trying to get the name of a method on a type using a lambda expression. I'm using Windows Identity Foundation and need to define access policies with the type name with namespace as a resource and the method name as the action. Here is an example. This is the type I would be getting the type name and method name from: namespace My....

Convert a C# Method to return a boolean from a lambda expression

The following obviously will not compile, so what should I change? public bool IsFoobar(bool foo, bool bar) { return db.Foobars.SingleOrDefault(fb => ((fb.foo == foo) && (fb.bar == bar))); } ...

Multiple Order By with LINQ

I start with a basic class that I want to manipulate in a List using LINQ, something like the following: public class FooBar { public virtual int Id { get; set; } public virtual string Foo{ get; set; } public virtual string Bar{ get; set; } } This is what I ultimately found out to solve my problem using the non ...

What is the best way to filter the contents of an arraylist?

Say I have an ArrayList of USBDevice Objects. Each USBDevice has ProductID and VendorID properties (among others). I want to create another ArrayList that is a subset of the first that contains only USBDevice that match a specific VID. What is the shortest way of doing this? I haven't tried this yet but can lambda expressions be used lik...

C# to VB - How do I convert this anonymous method / lambda expression across?

How would you convert this to VB (using .NET 4.0 / VS2010) ? bw.DoWork += (o, args) => { Code Here }; I thought maybe like this: AddHandler bw.DoWork, Function(o, args) Code Here End Function But it says Function does not return a value on all code paths. Ideas? ...

Accessing member variables through boost lambda placeholder

I'm trying to print the second member variable of all items in an stl map using a lambda expression map<int, int> theMap; for_each(theMap.begin(), theMap.end(), cout << bind(&pair<int, int>::second, _1) << constant(" ")); but this is not compiling. I essentially want to de-reference the placeholder. Any idea what I'm missing...

Using 'return' in a Ruby block

I'm trying to use Ruby 1.9.1 for an embedded scripting language, so that "end-user" code gets written in a Ruby block. One issue with this is that I'd like the users to be able to use the 'return' keyword in the blocks, so they don't need to worry about implicit return values. With this in mind, this is the kind of thing I'd like to be...

Is the result of List<T>.FindAll guaranteed to be in the same order as the original list?

If I've got a List with the following entries: Apple Banana Grape Cherry Orange Kiwi Is the result of fruit.FindAll(f => f.Length == 6) guaranteed to always be Banana Cherry Orange or could the order be different? ...

How to use an Argument in NewExpression as Expression to be used in OrderBy method?

First, I use C# 4.0 and EF 4.0 with POCO object to access database. Next, I create some grid (like jqGrid) for displaying data from database via ASP.NET MVC 2.0. This grid can order data by clicking at the column header. Source code could look like this. // This method will generate data for jqGrid request. // jqGridRequest contain seve...

Using antlr and the DLR together -- AST conversion

I have an AST generated via ANTLR, and I need to convert it to a DLR-compatible one (Expression Trees). However, it would seem that i can't use tree pattern matchers for this as expression trees need their subtrees at instantiation (which i can't get). What solution would be best for me to use? ...

Lamda Explanation and what it is as well as a good example

Can anyone give me a good explanation of how to use Lamda and give a good example. I have seen it but I dont know what it is or does. ...

Unable to instantiate function templates which uses decltype to deduce return type, if called from inside a lambda?

I'm trying to use C++0x, and in particular lambda expression and decltype to simplify some of my code, using the MSVC10 RC compiler. I've run into the following very odd problem: template <typename F> auto foo(F f) -> decltype(f()){ return f(); } template <typename F> void bar(F f){ f(); } int main() { bar([](){ foo([]() { ...

Can't catch exception thrown by Invoke on a compiled expression

In the class: private Func<T, object> pony; In my function: object newValue; try { newValue = pony.Invoke(model as T); // This is the line where I get an exception! } catch (Exception exception) { // This code is never run, even though I get an exception two lines up! if(exception is DivideByZeroException) throw new DivideByZer...

C++ templated functor in lambda expression

This first piece has been solved by Eric's comments below but has led onto a secondary issue that I describe after the horizontal rule. Thanks Eric! I'm trying to pass a functor that is a templated class to the create_thread method of boost thread_group class along with two parameters to the functor. However I can't seem to get beyond m...

How do I Emit a System.Linq.Expression?

I've got some code that generates various Func<> delegates using System.Linq.Expressions and Expression.Lambda<Func<>>.Compile() etc. I would like to be able to serialize the generated functions into an assembly for later use. In the past I've done some stuff with System.Reflection.Emit but now that Linq Expressions I'd rather not go tha...

Why this Lambda function won't work in ASP.NET

Dictionary<string, int> myList = new Dictionary<string, int>(); List<KeyValuePair<string, int>> result = new List<KeyValuePair<string, int>>(myList); result.Sort((first, second) => second.Value.CompareTo(first.Value)); it throws 5 errors on line 3 while building here's the screenie shottie from ASP.NET 2.0 this is from the Console a...

Joining lambda Expressions

Hello, How would I go about joining two lambda expressions like theese: Expression<Func<string, bool>> expr1 = a => a.Length > 100; Expression<Func<string, bool>> expr2 = b => b.Length < 200; ... into an expression like this: Expression<Func<string, bool>> expr3 = s => s.Length < 100 && s.Length < 200; That is, joining them with a...

Emulate break statement in ForEach function

This is more of a hypothetical question as I am using .NET 3.5 more and more along with lambda expressions and anonymous delegates. Take this simple example: static void Main(string[] args) { List<int> numList = new List<int>(int[] { 1, 3, 5, 7, 9, 11, 12, 13, 15 }); numList.ForEach(i => { if (i % 2 == 1) Co...