lambda

How to Load type from string directly into linq expression in C# ?

This is the definition: public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source); How to replace TResult with something like: Type.GetType("MyClass from assembly"); ...

How to write this in better way?

Hi all. Let's look at this code: IList<IHouseAnnouncement> list = new List<IHouseAnnouncement>(); var table = adapter.GetData(); //get data from repository object -> DataTable if (table.Rows.Count >= 1) { for (int i = 0; i < table.Rows.Count; i++) { var anno = new HouseAnnouncement(); anno.Area = float.Parse(tab...

how to escape key words in c# ?

I want to do the equivalent of the following VB in c# Function([class]) "hello" This would be the same as this in c# class=>"hello" The problem is that the word class is a key word in the language. But I want to use it as a variable name. In the VB example you can use the [] brackets to 'escape' that key word and allow it to be u...

Access to modified closure, is this a ReSharper bug?

I have the latest ReSharper 5.0 build (1655), where I have encountered the suggestion 'Access to modified closure' on the following code: var now = new DateTime(1970, 1, 1); var dates = new List<DateTime>(); dates.Where(d => d > now); ... now = new DateTime(); and the now inside the lambda expression is underlined with the warning. I...

Mocking lambda in rhino mocks

I am trying to use Rhino Mocks to mock the following lambda, but keep hitting a brick wall var result = rep.Find<T>(x => (x as IEntity).ID == (entity as IEntity).ID).FirstOrDefault(); Any ideas? ...

Using deprecated binders and C++0x lambdas

C++0x has deprecated the use of old binders such as bind1st and bind2nd in favor of generic std::bind. C++0x lambdas bind nicely with std::bind but they don't bind with classic bind1st and bind2nd because by default lambdas don't have nested typedefs such as argument_type, first_argument_type, second_argument_type, and result_type. So I...

Using a message class static method taking in an action to wrap Try/Catch

I have a Result object that lets me pass around a List of event messages and I can check whether an action was successful or not. I've realized I've written this code in alot of places Result result; try { //Do Something ... //New result is automatically a success for not having any errors in it result = new Result()...

Using a linq or lambda expression in C# return a collection plus a single value

I'd like to return a collection plus a single value. Presently I am using a field to create a new list adding a value to the list and then returning the result. Is there a way to do this with a linq or lambda expression? private List<ChargeDetail> _chargeBreakdown = new List<ChargeDetail>(); public ChargeDetail PrimaryChargeDetail ...

Using linq to combine objects

I have 2 instances of a class that implements the IEnumerable interface. I would like to create a new object and combine both of them into one. I understand I can use the for..each to do this. Is there a linq/lambda expression way of doing this? EDIT public class Messages : IEnumerable, IEnumerable<Message> { private List<Message> ...

Help with python list-comprehension

A simplified version of my problem: I have a list comprehension that i use to set bitflags on a two dimensional list so: s = FLAG1 | FLAG2 | FLAG3 [[c.set_state(s) for c in row] for row in self.__map] All set_state does is: self.state |= f This works fine but I have to have this function "set_state" in every cell in __map. Every c...

Use LINQ and lambdas to put string in proper case

I have this function called ProperCase that takes a string, then converts the first letter in each word to uppercase. So ProperCase("john smith") will return "John Smith". Here is the code: public string ProperCase(string input) { var retVal = string.Empty; var words = input.Split(' '); foreach (var wo...

lambda traits inconsistency across C++0x compilers

I observed some inconsistency between two compilers (g++ 4.5, VS2010 RC) in the way they match lambdas with partial specializations of class templates. I was trying to implement something like boost::function_types for lambdas to extract type traits. Check this for more details. In g++ 4.5, the type of the operator() of a lambda appears...

Access the value of a member expression

If i have a product. var p = new Product { Price = 30 }; and i have the following linq query. var q = repo.Products().Where(x=>x.Price == p.Price).ToList() In an IQueryable provider, I get a MemberExpression back for the p.Price which contains a Constant Expression, however I can't seem to get the value "30" back from it. Update I...

Anonymous Methods / Lambda's (Coding Standards)

In Jeffrey Richter's "CLR via C#" (the .net 2.0 edtion page, 353) he says that as a self-discipline, he never makes anonymous functions longer than 3 lines of code in length. He cites mostly readability / understandability as his reasons. This suites me fine, because I already had a self-discipline of using no more than 5 lines for an ...

C# going nuts when I declare variables with the same name as the ones in a lambda

I have the following code (generates a quadratic function given the a, b, and c) Func<double, double, double, Func<double, double>> funcGenerator = (a, b, c) => f => f * f * a + b * f + c; Up until now, lovely. But then, if i try to declare a variable named a, b, c or f, visual studio pops a "A local variable named 'f' could not be decla...

How to replace for-loops with a functional statement in C#?

A colleague once said that God is killing a kitten every time I write a for-loop. When asked how to avoid for-loops, his answer was to use a functional language. However, if you are stuck with a non-functional language, say C#, what techniques are there to avoid for-loops or to get rid of them by refactoring? With lambda expressions and...

When is LINQ (to objects) Overused?

My career started as a hard-core functional-paradigm developer (LISP), and now I'm a hard-core .net/C# developer. Of course I'm enamored with LINQ. However, I also believe in (1) using the right tool for the job and (2) preserving the KISS principle: of the 60+ engineers I work with, perhaps only 20% have hours of LINQ / functional par...

lambdas and attributes in c#

Can you use a lambda expressions as an argument to an attribute? The motivation I am thinking of would be to eliminate reliance on magic strings Cheers, Berryl ...

.NET C# setting the value of a field defined by a lambda selector

I have a generic class HierarchicalBusinessObject. In the constructor of the class I pass a lambda expression that defines a selector to a field of TModel. protected HierarchicalBusinessObject (Expression<Func<TModel,string>> parentSelector) A call would look like this, for example: public class WorkitemBusinessObject : Hie...

LINQ vs Lambda vs Anonymous Methods vs Delegates

Can anyone explain what are the LINQ, Lambda, Anonymous Methods, Delegates meant? How these 3 are different for each other? Was one replaceable for another? I didn't get any concrete answer when i did Googling ...