lambda

Selecting Many Fields From a Table using Linq and Lamda Expressions

I have a DataContext (db) that can access the tables in my SQL Express database, from which I would like to extract only three of the multiple fields in the tblItem table: // this does not work - what is the correct way to do it? var items = db.tblItems.Select(i => i.id && i.name && i.totalAmount); The intention is to spit these out...

Capturing Inline Expression Result

What I need to do is capture the result of an expression within an MVC view (sample below). I have also provided a stub Processor demonstrating what I want to achieve. Basically I want to divert the Target of the Action into some arbitrary string buffer that I can manipulate later. However the Target property is readonly. Can this ...

Expression Tree Creation and ExpressionTree Convert Type

lets say i have : anything.where(x=>x.age == int.parse(txtage.text)); now i know that int.parse(txtage.text) is an expression of type ExpressionType.Convert now i wanna know how to create an expression of type ExpressionType.Convert manually (programatically) why ? because im passing expressions between layers and changing the ty...

How call a custom method in lambda expression . I Use Entity Framework 4 . Stored expression error

Is it possible to call a custom method in a lambda expression.? //Address a : It's an Entity public string AddressConstructor(Address a) { return a.City + "," + a.Province; } var Test = _db.MyTableTest.Select( t => new ViewModel { MyID = t.ID, ...

capture member variable by value

How would I catch a member variable by value when using C++0x lambdas? Using the [=my_member] syntax doesn't seem to work, and implicit capture uses the "this" pointer. What is need is a way to explicitly specify capture type of member variables. Is that possible? My workaround for now is: void member_function() { std::shared_ptr<...

Asterisk in Block Parameters List

In Ruby, I have code similar to the following foo { |x, y| puts y } Because the compiler/interpreter keeps warning me about the unused var X, I replaced x with a '*' and the compiler stopped complaining. (I don't know why I decided * was the best choice... It just happened...) foo { |*, y| puts y } What does this do exactly? And a...

Discovery of op_Addition OR implementing method for Expression.Add

I'm writing a language using Antlr and Expression trees. I've defined a standard factory method for my Tree Parser to use when generating addition, and it works very nicely for the built in integral types, now I'm moving on to more general types. At the moment it's incredibly naive, it simply does this (in-progress TDD code often looks...

Passing 'None' as function parameter (where parameter is a function)

I am writing a small app that has to perform some 'sanity checks' before entering execution. (eg. of a sanity check: test if a certain path is readable / writable / exists) The code: import logging import os import shutil import sys from paths import PATH logging.basicConfig(level=logging.DEBUG) log = logging.getLogger('sf.core.sanity...

How to replace lambda written in Where clause of Linq with equivalent delegate

I have an Query expression that uses a predicate type and lambda expression. I am getting desired result with this. But I am not clear with how this expression is getting evaluated. I tried to break this lambda expression by creating delegate and replacing condition under Where with delegate type. If I have to rewrite the same thing wi...

How could I extract the member path from this lambda expression.

I have a method to add a date condition to my linq query. What I want to do is pass x.Due in as a parameter to make this able to work with any date. Any Ideas? protected virtual IQueryable<TaskView> AddTaskDuePredicate( DateCriteria dateCriterion, IQueryable<TaskView> taskSummary ) { if ( dateCriterion.Condition == DateConditi...

Can I use a lambda expression with params keyword?

Lets say I have the following code: delegate int MyDel (int n); // my delegate static int myMethod( MyDel lambda, int n) { n *= n; n = lambda(n); return n; // returns modified n } This way, having different lambda expression I can tune the output of the Method. myMethod ( x => x + 1, 5); myMethod ( x => x - 1, 5)...

EF 4 LINQ Expression load items based on related entity

Here is the expression x => x.stf_Category.CategoryID == categoryId x refers to an Product Entity that contains a Category. I am trying to load all Products that match given categoryId. In the db the Product table contains a Foreign Key reference to Category (via CategoryId). Question: I think I am doing it wrong. Is there somethi...

How to get the index of a value in a vector in C++0x for_each algorithm and lambda expression?

I have the following C++0x code (compiler- MSVC++ 10): std::vector<float> data; data.push_back(1.0f); data.push_back(1.0f); data.push_back(2.0f); // lambda expression std::for_each(data.begin(), data.end(), [](int value) { // Can I get here index of the value too? }); What I want in the above code snippet is to get the index of ...

Declaring func<in T,Out Result> dynamically

Consider this: var propertyinfo = typeof(Customer).GetProperty(sortExpressionStr); Type orderType = propertyinfo.PropertyType; now i want to declare Func<int,orderType> i know its not possible directly since ordertype is at runtime but is there is any workarround ? this is exactly what i want to do : var propertyinfo = t...

How to test lambda functions with Moq?

There is a function: public class MyCacheClass : ICache { public void T GetObject<T>(Func<T> func) { T t; ... t = func(); ... return t; } } public class MyWorkClass : IWork { public Object MyWorkMethod(string value) { return new object(); } } These functions are ...

Why my LINQ expression do not work in LINQ to SQL?

private System.Linq.Expressions.Expression<Func<ActionLogs, bool>> GetExpression() { Expression<Func<ActionLogs, bool>> expr = w => w.ID != -1; if (ActionDate != null) { Expression<Func<ActionLogs, bool>> byDate = w => w.DateAction == ActionDate; var body = Expression.AndAlso(expr.Body, byDate.Body); e...

How can I build a recursive function that builds an MvcHtmlString Breadcrumb trail when I don't know the object beforehand?

I believe that I'm going to need to use an html helper method that uses lambda expressions to accomplish this. I was thinking of doing something along these lines: public static MvcHtmlString GetCategoryBreadCrumbs<T>( this HtmlHelper html, IEnumerable<T> currentCat, Func<T, T> parentProperty, Func<T...

How would I use a linq expression to create a delegate with parameters?

Hi, I found your code example below extremely helpful, however I don't understand the use of expressions in the code... I am looking to modify this method to accept a delegate with a params object[] parameter.. any pointers would be greatly appreciated. Simon Bridge ([email protected]) class EventProxy { static public Delegate C...

c# lambda expression + reflection questions

This is mostly for educational purposes. I'm trying to create the InputMapper class, which is used in this example: var mapper = new InputMapper<SomeType>(); mapper.Map("some user input", someType => someType.IntProperty, "Input was not an integer"); mapper.Map("some user input", someType => someType.BoolProperty, "Input was not a bool"...

How to sort with lambda

Trying to sort by date with lambda I can't understand which lambda my error message is referring to. The message is <lambda>() takes exactly 1 argument (2 given) The 2 instructions are a = A.proximity_fetch(A.all().filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified") ,db.GeoPt...