lambda

Left join using LINQ

Could someone give me an example of how to perform a left join operation using LINQ/lambda expressions? ...

Simple MVC NerdDinners Lambda Question

In this code from Microsoft's MVC Tutorial NerdDinners: public class DinnerRepository { private NerdDinnerDataContext db = new NerdDinnerDataContext(); // // Query Methods public IQueryable<Dinner> FindAllDinners() { return db.Dinners; } public IQueryable<Dinner> FindUpcomingDinners() { return from dinner in db.Dinners ...

Does C# Pass by Value to Lambdas?

I have some code, int count = 0; list.ForEach(i => i.SomeFunction(count++)); This seems to not increment count. Is count passed by value here? Is there any difference if I use the {} in the lambda? int count = 0; list.ForEach(i => { i.SomeFunction(count++); }); Update 1 Sorry, my mis...

C# How to create extension methods with lambda expressions

Currently i am creating an extension method that accepts parameters. Using the below example, how could one convert this using lambda expressions? public static decimal ChangePercentage(this IEnumerable<Trade> trades, DateTime startDate, DateTime endDate) { var query = from trade in trades where trade.TradeTime >=...

Using Lambda in Unit Test in VB.NET 2008 with Rhino.Mocks

Hi, I am trying to create a unit test similar to how I would have done one in C# but am struggling with the lambdas in vb. Bascially I am trying to mock a class and then create a stub and return. In C# I would have done something like; MockedPersonRepository .Stub(x => x.Find(id)) .Return(person) But in visual basic I am t...

Is there an easy way to append lambdas and reuse the lambda name in order to create my Linq where condition?

I have a user control which takes a Func which it then gives to the Linq "Where" extension method of a IQueryable. The idea is that from the calling code, I can pass in the desired search function. I'd like to build this search function dynamically as such: Func<Order, bool> func == a => true; if (txtName.Text.Length > 0) { //add it...

How to check for nulls in a deep lambda expression?

How can I check for nulls in a deep lamda expression? Say for example I have a class structure that was nested several layers deep, and I wanted to execute the following lambda: x => x.Two.Three.Four.Foo I want it to return null if Two, Three, or Four were null, rather than throwing a System.NullReferenceException. public class Test...

What design pattern should I use to create an easy binding map between a query and textboxes for Linq search screens?

Over and over, I find myself developing WinForm business application screens that have a bunch of text boxes for search criteria, then a search button. These are mapped into an expression using Linq, then passed onto my Linq2Sql layer. I'd like to create an easy way to "bind" these textboxes to the underlying query using different opti...

linq - Substitute Non-Generic Lambda Expression for Generic Lambda Expression

I have a this line of code: var predicate = Expression.Lambda<Func<TEntityType, bool>>(body, param); where TEntityType is a generic parm. However, I don't have generic parm available. I do have: Type _EntityType; What is the non-generic syntax for Expression.Lambda is this case? Thanks ...

Accessing value from expression

public Method1(Expression<Func<T, TProperty>> valueToCompare) { //Examine expression } public Method1(TProperty valueToCompare) : this(x => valueToCompare) {} and i run them like this Method1(x => 1); and Method1(1); If i examine the expression when the first overload is called then I get a constant expression. However when ...

Lambda Expression using Foreach Clause...

EDIT For reference, here's the blog post which eric referrrred to in the comments http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx ORIG More of a curiosity I suppose but one for the C# Specification Savants... Why is it that the ForEach() clause doesn't work (or isn't available) for use on IQueryable/IEnu...

Trying to get generic when generic is not available.

I am trying to develop some general purpose custom ValidationAttributes. The fact that one cannot create a generic subclass of an attribute is making me nuts. Here is my code for the IsValid override in a ValidationAttribute to verify that the value of a property is unique: public override bool IsValid(object value) { S...

ValidationAttribute to ensure property value is unique.

See Ref to: http://stackoverflow.com/questions/858987/trying-to-get-generic-when-generic-is-not-available I am trying to write a ValidationAttribute that will ensure that a property value is uniqure: [UniqueValueValidationAttribute] object SomeProperty {get; set;} The attribute knows (or can determine by reflection): The class (Ta...

UnHooking Events with Lambdas in C#

I often run into a situation where I want to subscribe to an event, but I want to use a lambda to do so: public class Observable { public event EventHandler SomethingHappened; public void DoSomething() { // Do Something... OnSomethingHappened(); } } // Somewhere else, I hook the event observable.Somethi...

Returning a nested generic Expression<Func<T, bool>>

The error message is "The type or namespace name 'T' could not be found." ??? public static Expression<Func<T, bool>> MakeFilter(string prop, object val) { ParameterExpression pe = Expression.Parameter(typeof(T), "p"); PropertyInfo pi = typeof(T).GetProperty(prop); MemberExpression me = Expression.MakeMemberAccess(pe, pi); ...

ValidationAttribute Redux.

Ref to:Creating a ValidationAttribute to ensure unique column values. Ok... Let's try reframing the question: from here I have ripped this code: static TEntity Get<TEntity, TKey>(this DataContext ctx, TKey key) where TEntity : class { var table = ctx.GetTable<TEntity>(); var pkProp = (from member in ctx.Mapping...

Sort Generic list on two or more values

We have a generic List(Of Product) that must be sorted on two or more properties of the Product class. The product class has the properties "Popular" numeric (asc), "Clicked" numeric (desc), "Name" string (asc). In order of naming the properties we want the list to sort. How can it be sort with an lamba statement? If have found to sort...

What is the => token called?

The => token is part of the C# 3.0 lambda syntax. My efforts to find the name of this token have failed so far. ...

HashSet constructor with custom IEqualityCompare defined by lambda?

Currently the HashSet<T> constructor that allows you to define your equality comparison yourself is the HashSet<T>(IEqualityComparer<T> comparer) constructor. I would like to define this EqualityComparer as a lambda. I found this blog post that has made a class that allows you to generate your comparer through lambda and then hides th...

Does this Asynchronous Lambda Cryptica Code do what I think?

Action<SPItemEventProperties> deleteAction = DeleteWorkspace; AsyncCallback deleteDone = deleteAction.EndInvoke; SPSecurity.RunWithElevatedPrivileges(() => deleteAction.BeginInvoke(properties, deleteDone, null)); So this is suppose to call DeleteWorkspace Asynchronously and then call EndInvoke when its done, I wrote it but ...