lambda

Comparison function in Python using Lambdas

I am trying to understand lambdas and I get the idea but how do I define multiple conditions for a Point2 [x,y] comparison, so something like: if x1 < x2: -1 if x1 == x2: 0 if x1 > x2: 1 ...

Lambda "cannot be inferred from the usage"

I have the following dictionary declared: private readonly Dictionary<int, Image> dictionary; And I have a method, which is causing a compiler error: public IQueryable<Image> Find(Func<Image, bool> exp) { return dictionary.Single(exp); } The error i get is: Error 1 The type arguments for method 'System.Linq...

C# LINQ Where Predicate Type Arguments

I have an XElement with values for mock data. I have an expression to query the xml: Expression<Func<XElement, bool>> simpleXmlFunction = b => int.Parse(b.Element("FooId").Value) == 12; used in: var simpleXml = xml.Elements("Foo").Where(simpleXmlFunction).First(); The design time error is: The type arguments for method 'Sy...

Lambda Expression cause weakreference's target cannot be GC?

namespace Test { class Test { delegate void HandleMessage(string message); public void handleMessage(string message){} static void Main(string[] args) { HandleMessage listener1 = new Test().handleMessage; WeakReference w1 = new WeakReference(listener1); HandleMessage listener2 = (message) => { ...

Is there a nicer/inline way of accomplishing the following in C# / LINQ?

Often I find myself filling ASP.NET repeaters with items that need the CSS class set depending on index: 'first' for index 0, 'last' for index (length-1), and 'mid' in the middle: _repeater.DataSource = from f in foos select new { ..., CssClass = MakeCssClass( foos, f ) }; privat...

how to create expression tree / lambda for a deep property from a string

Hi, Given a string: "Person.Address.Postcode" I want to be able to get/set this postcode property on an instance of Person. How can I do this? My idea was to split the string by "." and then iterate over the parts, looking for the property on the previous type, then build up an expression tree that would look something like (apologies f...

C# Expression Tree Parameter for Parent Navigation Property

How can I create a ParameterExpression for the parent side of a 1 to * Navigation Property? The following works for the child entity: var parameter = Expression.Parameter( typeof(T), // where T is the entity type GetParameterName()); // helper method to get alias Trying something similar on TParent produces a query originatin...

LINQ to SQL extension method for sorting and paging

I have found an extension method that handles sorting and paging for LINQ. While this works well, I am trying to see if there are some other ways I can use this. At present, the code for the extension method is as follows: public static IQueryable<T> Page<T, TResult>( this IQueryable<T> obj, int page, int pageSize, System.Li...

Using lambda expressions to get a subset where array elements are equal

I have an interesting problem, and I can't seem to figure out the lambda expression to make this work. I have the following code: List<string[]> list = GetSomeData(); // Returns large number of string[]'s List<string[]> list2 = GetSomeData2(); // similar data, but smaller subset &nbsp; List<string[]> newList = list.FindAll(predicate(st...

LINQ Query skips without exception. Why?

I have a method that gets a nested array as parameter: Number[][] data where Number is my very simple class that inherits from INotifyPropertyChange. And then i have a statement like this: double[] max = (double[])data.Select(crArray => crArray.Select( cr => cr.ValueNorm ).Max()); When I'm trying to watch it in the debugger it jus...

How do I combine two Member Expression Trees?

I'm trying to combine the following expressions into a single expression: item => item.sub, sub => sub.key to become item => item.sub.key. I need to do this so I can create an OrderBy method which takes the item selector separately to the key selector. This can be accomplished using one of the overloads on OrderBy and providing an ICompa...

C#: No implicit conversion between 'lambda expression' and 'lambda expression'?

Type of conditional expression cannot be determined because there is no implicit conversion between 'lambda expression' and 'lambda expression' Say whaat? Could someone please explain this compile error to me? This is the code that produces it: protected override Func<System.IO.Stream> GetStream() { return someBool ...

Events in lambda expressions - C# compiler bug?

I was looking at using a lamba expression to allow events to be wired up in a strongly typed manner, but with a listener in the middle, e.g. given the following classes class Producer { public event EventHandler MyEvent; } class Consumer { public void MyHandler(object sender, EventArgs e) { /* ... */ } } class Listener { p...

Strange behavior when using lambda expression on WPF buttons click event.

My problem is hard to explain, so I created an example to show here. When the WPF window in the example below is shown, three buttons are displayed, each one with a different text. When anyone of these buttons is clicked, I assume its text should be displayed in the message, but instead, all of them display the same message, as if all ...

Is there a way I can dynamically define a Predicate body from a string containing the code?

This is probably a stupid question, but here goes. I would like to be able to dynamically construct a predicate < T > from a string parsed from a database VARCHAR column, or any string, for that matter. For example, say the column in the database contained the following string: return e.SomeStringProperty.Contains("foo"); These code...

LINQ Expression to return Property value?

I'm trying to create a generic function to help me select thousands of records using LINQ to SQL from a local list. SQL Server (2005 at least) limits queries to 2100 parameters and I'd like to select more records than that. Here would be a good example usage: var some_product_numbers = new int[] { 1,2,3 ... 9999 }; Products.SelectByP...

C#, Linq2Sql: Is it possible to concatenate two queryables into one?

I have one queryable where I have used various Where and WhereBetween statements to narrow the collection down to a certain set. Now I need to add kind of a Where || WhereBetween. In other words, I can't just chain them together like I have up till now, cause that will work as an And. So, how can I do this? I see two possibilities: Cr...

What does the operator '=>' mean in C#?

What does the '=>' in this statement signify? del = new SomeDelegate(() => SomeAction()); Is the above declaration the same as this one? del = new SomeDelegate(this.SomeAction); Thanks. ...

How do I include an InvocationExpression that is only evaluated once in an expression tree for linq to sql?

I'm trying to manually combine expression trees to accomplish a level of modularity that seems to allude me using the standard linq operators. The code essentially creates an expression tree that uses one expression to decide which of two other expressions to call. One of the other expressions requires an extra parameter that is itself o...

Why can I not edit a method that contains an anonymous method in the debugger?

So, every time I have written a lambda expression or anonymous method inside a method that I did not get quite right, I am forced to recompile and restart the entire application or unit test framework in order to fix it. This is seriously annoying, and I end up wasting more time than I saved by using these constructs in the first place. ...