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 ...
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 ...
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...
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...
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) => { ...
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...
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...
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...
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...
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 List<string[]> newList = list.FindAll(predicate(st...
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...
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...
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 ...
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...
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 ...
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...
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...
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 '=>' in this statement signify? del = new SomeDelegate(() => SomeAction()); Is the above declaration the same as this one? del = new SomeDelegate(this.SomeAction); Thanks. ...
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...
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. ...