lambda

get end values from lambda expressions method parameters

basically I want to get the values of the parameters of a called method like this: var x = 1; var a = 2; var b = 3; Do<HomeController>(o => o.Save(x, "Jimmy", a+b+5, Math.Sqrt(81))); public static void Do<T>(Expression<Action<T>> expression) where T : Controller { // get the values 1,Jimmy,10,9 here } ...

How to read File names recursively from subfolder using LINQ

How to read file name with dll extension from a directory and from its subfolders recursively using LINQ or LAMBDA expression. Now i'm using Nested for-each loop to do this. Is there any way to do this using LINQ or LAMBDA expression? ...

Lambda capture as const reference?

Is it possible to capture by const reference in a lambda expression? I want the assignment marked below to fail, for example: #include <cstdlib> #include <vector> #include <string> #include <algorithm> using namespace std; int main() { string strings[] = { "hello", "world" }; static const size_t num_st...

LINQ Join (Left Outer) with Take(1)

I have the below LINQ that is returning zero IF there aren't any Addresses(Inner Join). How would I make this an Outer Join and then only Take(1)? var results = query.Join( DB.tblAddresses.Where(t => t.AddressTypeID == 'm' || t.AddressTypeID == 'b').OrderByDescending(x => x.AddressTypeID), p => p.PersonI...

Get string property name from expression

I'm trying to write a strongly typed helper which would be something like this: Html.Lookup(x => x.FooId); for now I have this: public static MvcHtmlString Lookup<T,TReturn>(this HtmlHelper<T> html, Func<T, TReturn> expression) { // get string "FooId" here } anybody knows how to get this ? ...

Compiled LinQ query with with expressions in functions

I would like to create a compiled query which uses reusable where predicates. An example to make this clear: ObjectContext.Employees.Where(EmployeePredicates.CustomerPredicate) EmployeePredicates is a static class with a property CustomerPredicate that looks like this: public static Expression<Func<Employee, bool>> CustomerPredicate ...

Linq: Dynamic Query Contruction: query moves to client-side

Hi all, I've been following with great interest the converstaion here: http://stackoverflow.com/questions/3769141/construct-query-with-linq-rather-than-sql-strings with regards to constructing expression trees where even the table name is dynamic. Toward that end, I've created a Extension method, addWhere, that looks like: static pub...

Strange cast error with Linq and Oracle

I am getting a cast error with Linq to a DataTable that was populated with Oracle. bool isTrue = DataTable.AsEnumerable().Any (x => x.Field<int>("MYNUMBERFIELD") == MYNUMBER); In SQL this works fine as expected. In Oracle is fails with a cast error on the cast. In C# code the same thing happens when you do the following: i...

Python "switch statement" and string formatting

Hello guys, I'm trying to do switch statement (with dictionary) and the answer needs to be a formatted string, so for example: descriptions = { 'player_joined_clan': "%(player)s joined clan %(clan)s." % {"player": token1, "clan": token2}, #etc... } Now, this would work if those both tokens were always defined, which is not the...

get values from a nested dictionary using lambda values of the dictionary are ArrayList of ArrayList

I need to retrieve values from dictionary which stores a ArrayList which in turn has an ArrayList This second ArrayList has int array stored . Now how can I retrieve those integer values. ` Dictionary<int, ArrayList> obj = new Dictionary<int, ArrayList>(); ArrayList objList1 = new ArrayList(); ArrayList objLi...

how to 'not' a lambda expression for entity framework

Hi Given the following Expression<Func<T,bool>> matchExpression; How can i create another expression that is a 'not' of the existing one. i have tried Expression<Func<T, bool>> func3 = (i) => !matchExpression.Invoke(i); but this is not supported by the entity framework... Regards ...

winform combobox lambda expression

I have a combobox in vs 2010 using vb.net. What i'd like to do is use a ilist to drive a combobox. I have this working but when i try to order the combobox using a lambda expression, nothing shows up in the combobox. With Me.cbAgency .DataSource = Os.OrderBy(Function(o) o.Agency) .DisplayMember = "Agency" .Ta...

Expression Tree Dependency Analyzer

I'm building an expression tree dependency analyzer for a cross data source IQueryProvider. That is, I have an IQueryable with some elements that can be executed locally in memory against some arbitrary provider (say Entity Framework). Some other elements in the IQueryable go against an entity that I need to make a remote WCF call. The ...

How does linq lambdas work inside a loop?

I'm trying to implement Tasks im my Application. Here's a sample code: There's one simple Interface I, 3 classes are derived form it (A,B,C) I create a list of Is, poplualte it with A, B, C instances, and then create a tasf for each other to call method do1(); interface I { void do1(); } class A : I { ...

Lambda Expressions

Is there a better way to avoid NullRefenrenceException other than try/catch i.e x => x.Child.GChild.GChildProp1 If Child Or GChild happen to be null obviously this would raise NullRefenrenceException other solution that popped into the head, is to evaluate each expression part individually from left ? which would be better or i...

Nesting Function Calls

Hey all, This has been driving me absolutely nuts. I have a substitute function like this: (define (mysub x bind body) ;; x, bind, body are lists ...) I need to call the function like this: ;;this is the explicit call for when length x = length bind = 2. ;;how do I generalize these nested calls? ;;in case it's not obvious, i'm ...

Reverse of Expression<Func<T,TResult>>.Compile()?

Since we can: Expression<Func<int, bool>> predicate = x => x > 5; var result = Enumerable.Range(0,10).Where(predicate.Compile()); How can I: Func<int,bool> predicate = x => x > 5; Expression<Func<int,bool>> exp = predicate.Decompile(); That is, I want to get the corresponding Expression of the Func. Is it possible? ...

How to use a lambda expression as a template parameter?

How to use lambda expression as a template parameter? E.g. as a comparison class initializing a std::set. The following solution should work, as lambda expression merely creates an anonymous struct, which should be appropriate as a template parameter. However, a lot of errors are spawned. Code example: struct A {int x; int y;}; std::s...

How to get duplicate items from a list using LINQ ?

I'm having a List<string> like: List<String> list = new List<String>{"6","1","2","4","6","5","1"};` I need to get the duplicate items in the list into a new list. Now i'm using nested for loop to do this. The Resulting list will contain {"6","1"} Is there any idea to do this using LINQ or Lambda expression? ...

Difference between delegate {} and (input parameters) => {}

Hi all, I have a few methods like this public void DoSomething(Action<int> action) { ... } In some cases I do not use the parameters passed into action. Are there any differences I should be aware of between calling it like this DoSomething(delegate { ... }); or DoSomething(_ => { ... }); ...