lambda

C# - closures over class fields inside an initializer?

Consider the following code: using System; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { var square = new Square(4); Console.WriteLine(square.Calculate()); } } class MathOp { protected MathOp(Func<int> calc) { _calc ...

Problem with nested lambda expressions.

Hey I'm trying to do a nested lambda expression like to following: textLocalizationTable.Where( z => z.SpokenLanguage.Any( x => x.FromCulture == "en-GB") ).ToList(); but i get the error: Member access 'System.String FromCulture' of 'DomainModel.Entities.SpokenLanguage' not legal on type 'System.Data.Linq.EntitySet`1[DomainM...

View Lambdas in Visual Studio Debugger

I have the a simple LinqToSQL statement that is not working. Something Like this: List<MyClass> myList = _ctx.DBList .Where(x => x.AGuidID == paramID) .Where(x => x.BBoolVal == false) .ToList(); I look at _ctx.DBList in the debugger and the second item fits both parameters. Is there a way I can dig into this more ...

Create a ruby Proc from a string

I want to define the block as a string, then create the lambda. The following example does not work. Is something like this possible? code_string = "|x|x*2" l = lambda {eval(code_string)} l.call(3) => 6 ...

Adding LambaExpression to an instance of IQueryable

ParameterExpression parameter = Expression.Parameter(typeof(Product), "x"); MemberExpression Left = Expression.MakeMemberAccess(parameter, typeof(Product).GetProperty("Name")); ConstantExpression Right = Expression.Constant(value, typeof(String)); BinaryExpression expression = Expression.Equal(Left, Right); ...

Using lambda expressions for event handlers

I currently have a page which is declared as follows: public partial class MyPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //snip MyButton.Click += (o, i) => { //snip } } } I've only recently moved to .NET 3.5 from 1.1,...

Are lambda expressions/delegates in C# "pure", or can they be?

I recently asked about functional programs having no side effects, and learned what this means for making parallelized tasks trivial. Specifically, that "pure" functions make this trivial as they have no side effects. I've also recently been looking into LINQ and lambda expressions as I've run across examples many times here on StackOve...

Dynamic expression tree how to

Hello everybody! Implemented a generic repository with several Methods. One of those is this: public IEnumerable<T> Find(Expression<Func<T, bool>> where) { return _objectSet.Where(where); } Given <T> to be <Culture> it is easy to call this like this: Expression<Func<Culture, bool>> whereClause = c => c.Cu...

Lambda "if" statement?

I have 2 objects, both of which I want to convert to dictionarys. I use toDictionary<>(). The lambda expression for one object to get the key is (i => i.name). For the other, it's (i => i.inner.name). In the second one, i.name doesn't exist. i.inner.name ALWAYS exists if i.name doesn't. Is there a lambda expression I can use to combine...

lambda operator c# learning

Hello all Can you recommend me on places where i can read and see examples on usage of Lambda operator in c# . I will really like to see a lot examples on it... Thanks. ...

Anonymous recursive PHP functions.

Is it possible to have a PHP function that is both recursive and anonymous? This is my attempt to get it to work, but it doesn't pass in the function name. $factorial = function( $n ) use ( $factorial ) { if( $n == 1 ) return 1; return $factorial( $n - 1 ) * $n; }; print $factorial( 5 ); I'm also aware that this is a bad way t...

Multiple Conditions in Lambda Expressions at runtime C#

Hi, I would like to know how to be able to make an Expression tree by inputting more than one parameter Example: dataContext.Users.Where(u => u.username == "Username" && u.password == "Password") At the moment the code that I did was the following but would like to make more general in regards whether the condition is OR or AND pub...

How do I unit test a C# function which returns a Func<something>?

I have a class containing a method which returns a Result object which contains a property of type Func. class Result { public Func<Result> NextAction { get; set; } } How do I write a unit test assertion regarding the content of this Func? The following obviously does not work, because the compiler generates two different methods f...

Settings variable values in a Moq Callback() call

I think I may be a bit confused on the syntax of the Moq Callback methods. When I try to do something like this: IFilter filter = new Filter(); List<IFoo> objects = new List<IFoo> { new Foo(), new Foo() }; IQueryable myFilteredFoos = null; mockObject.Setup(m => m.GetByFilter(It.IsAny<IFilter>())).Callback( (IFilter filter) => myFilte...

Extracting pair member in lambda expressions and template typedef idiom

Hi, I have some complex types here so I decided to use nifty trick to have typedef on templated types. Then I have a class some_container that has a container as a member. Container is a vector of pairs composed of element and vector. I want to write std::find_if algorithm with lambda expression to find element that have certain value. T...

How to implement lambda as a function called "lambda" in Clojure?

I'd like to be able to define lambdas using common Lisp syntax, in Clojure. For example: (lambda (myarg) (some-functions-that-refer-to myarg)) This needs to result in the same as: #(some-functions-that-refer-to %) In my case, I know I'll always have exactly one arg, so perhaps that simplifies things. (But it can be called anythin...

Delegate within a delegate in VB.NET.

I am trying to write a VB.NET alternative to a C# anonymous function. I wish to call Threading.SynchronizationContext.Current.Send which expects a delegate of type Threading.SendOrPostCallback to be passed to it. The background is here, but because I wish to both pass in a string to MessageBox.Show and also capture the DialogResult I ne...

How do I get a value of a reference type in an Expression?

I have this method: public void DoSomething<T>(Expression<Func<T, object>> method) { } If this method is called like this: DoSomething(c => c.SomeMethod(new TestObject())); ... how do I get the value of the parameter that was passed into SomeMethod()? If the parameter is a value type, this works: var methodCall = (MethodCallExpre...

How can I filter a list of objects using lambda expression?

I know I shouldn't have id's with the same value. This is just fictitious, so overlook that. I have: List<Car> carList = new List<Car>(); carList.Add(new Car() { id = 1, name = "Honda" }); carList.Add(new Car() { id = 2, name = "Toyota" }); carList.Add(new Car() { id = 1, name = "Nissan" }); I want to use Lambda Expression to retriev...

Generating a LINQ query using Expression Trees

Update Thanks to Marc's help the AlphaPagedList class is now available on CodePlex if anyone is interested Original I'm trying to create an expression tree to return elements that start with a given charecter. IList<char> chars = new List<char>{'a','b'}; IQueryable<Dept>Depts.Where(x=> chars.Contains(x.DeptName[0])); I want this to...