lambda

Writing Lamdba Query Expressions in VB.Net using linq to sql provider

I'm just not understanding on how to create a lamdba expression instead of using a regular comprehension linq queries. All of the examples i see are not query a database using linq to sql. i want to able to construct a lambda expression that get my the orders from the northwind database where the country equals "us". I know how to constr...

Faster way to cast a Func<T, T2> to Func<T, object> ?

Is there a faster way to cast Fun<TEntity, TId> to Func<TEntity, object> public static class StaticAccessors<TEntity> { public static Func<TEntity, TId> TypedGetPropertyFn<TId>(PropertyInfo pi) { var mi = pi.GetGetMethod(); return (Func<TEntity, TId>)Delegate.CreateDelegate(typeof(Func<TEntity, TId>), mi); } public static Func<...

o => o.MethodWithParamters | is it possible to use a method in a lambda without () and paramteters

i have a method that takes as a parameter an expression because I need the method string name, and I don't care about the parameters of that method, is it possible to do that ? ...

LINQ to Entities - Support for Closures (Lambdas) ?

Hey all I've been working around a problem I have when using LINQ to Entities when using closures. Apparently, L2E does not support closures. Meaning: var users = from user in UserRepository.FindAll() select new UserDTO { UserId = user.UserId, Tickets = from ticket in TicketReposit...

lambda expressions on populated lists

There are a few posts on the site about how to order by using lambda expressions however I cannot seem to get mine to work. I am trying to reorder a list that is already populated. Am i wrong in thinking that i can rearrange the order of this list using lambada expressions? QuarterMileTimes.OrderByDescending(c => c.PquartermileTime); ...

Is it possible to construct a stringbuilder using a lambda function instead of foreach?

Possible Duplicate: LINQ to append to a StringBuilder from a String[] Forgive my functional programming noobiness, but is it even possible to use a lamba function to append each string in an array to a StringBuilder object? Is it possible to turn this code: // string[] errors = ... StringBuilder sb = new StringBuilder("<ul...

Rails Workflow Gem - Metaprogramming events into named_scopes?

I'm using http://github.com/geekq/workflow to provide a state machine. I'm using ActiveRecord to save state, which means I have a "workflow_state" attribute in the model. I think I want a named_scope for each event in the state machine, so I can find all objects in a given state. For example, assuming a very simple state machine: workfl...

Expression of type 'System.Int32' cannot be used for return type 'System.Object'

I am trying to produce a simple scripting system that will be used to print labels. I have done this in the past with reflection with no problem, but I am now trying to do it with Lambda functions so that I can cache the functions for reuse. The code I have so far is as follows... public static string GetValue<T>(T source, string prope...

Using Lambda expressions to order a list by GMT date

I have a list that contains dates that are in GMT format. What is the most elegant way to ensure that the following Lambda expression orders on the date field as GMT? ProductsList.OrderBy(Product => Product.Added).ToList(); ...

C# method accepting a predicate - does this look ok?

I'd like a method that has the following API: //get all users with a role of admin var users = myRepository.GetUsers(u => u.Role == Role.Admin); Will something like this work? IList<User> GetUsers(Func<User, bool> predicate) { var users = GetAllUsers(); return users.Where(predicate).ToList(); ...

Which assembly do I need to include for the C# where extension method?

What is the name of the assembly that must be included for the Where extension method? I can't find it's name on google, and apparently it's not one of these: using System; using System.Collections.Specialized; using System.Linq.Expressions; using System.Collections; using System.Collections.Generic; ...

C# Expression Trees and Invoking a Delegate

So I have a delegate which points to some function which I don't actually know about when I first create the delegate object. The object is set to some function later. I also then want to make an expression tree that invokes the delegate with an argument (for this question's sake the argument can be 5). This is the bit I'm struggling wi...

How is a LocalJumpError thrown from the following lambda ?

I've been reading the Ruby Programming Language book by Flanagan-Matz Context: Difference between Proc.new and lambda w.r.t return statements The book states that a return within a lambda should not raise a LocalJumpError (since lambdas are akin to method calls). A return in a lambda just exits the lambda - not the method enclosing the...

Python: defining functions on the fly

I have the following code: funcs = [] for i in range(10): def func(): print i funcs.append(func) for f in funcs: f() The problem is that func is being overriden. Ie the output of the code is: 9 9 9 ... How would you solve this without defining new functions? The optimal solution would be to change the name of th...

C# method group strangeness

I discovered something very strange that I'm hoping to better understand. var all = new List<int[]>{ new int[]{1,2,3}, new int[]{4,5,6}, new int[]{7,8,9} }; all.ForEach(n => n.ForEach(i => Console.WriteLine(i))); which can be rewritten as: ... all.ForEach(n => n.ForEach(C...

foreach(... in ...) or .ForEach(); that is the question

This is a question about coding for readability. I have an XDocument and a List<string> of the names of the elements that contain sensitive information that I need to mask (replace with underscores in this example). XDocument xDoc; List<string> propertiesToMask; This can be written in two ways, using traditional foreach loops, or usi...

Initialization of list with for_each to random variables

I'm trying to initialize a list to random integers using a for_each and a lambda function. I'm new to boost.lambda functions so I may be using this incorrectly but the following code is producing a list of the same numbers. Every time I run it the number is different but everything in the list is the same: srand(time(0)); theList.resiz...

Linq2Entities, many to many and dynamic where clause

Hi, Im fairly new to Linq and struggling using dynamic where over a many to many relationship. Database tables are like so: Products <-> Products_SubCategories <-> SubCategories with Products_SubCategories being a link table. My full linq statement is db.Products.Where("it.SubCategories.SubCategoryID = 2") .In...

When using AsynchCallback, where do you register the code to call when AsynchCallback has completed ?

Hi, I am following sample code on implementing MVVM in Silverlight (see: http://msdn.microsoft.com/en-us/magazine/dd458800.aspx). On page 5 (when printed), the author has the following segment of code: qry.BeginExecute(new AsyncCallback => a { try { IEnumerable<Game> results = qry.EndExecute(a); if (GameLoadin...

How to convert in Linq statement and assign correct value

Hello Friends, I been trying to write the following code in expression based way but not sure how can i do that assignment to the object after comparison. Any help would be highly appreciated. var pcs = from a in collection group a by a.TotType into g select new { TType = g.Key, SColl = g.Sel...