lambda

C#: How to test that an expression is short-circuited

I have an extension method with the following signature: public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { ... } I have written a test-case for it that makes sure the two expressions are in fact combined. At least so that the new expression I get works. Now I ...

Return an empty collection when Linq where returns nothing

I am using the below statement with the intent of getting all of the machine objects from the MachineList collection (type IEnumerable) that have a MachineStatus of i. The MachineList collection will not always contain machines with a status of i. At times when no machines have a MachineStatus of i I'd like to return an empty collection...

Converting Linq MemberExpression lambda to work on class with reference

For this question, I'll use the standard structure of Products (with an IsActive flag) and OrderItems (that each reference a Product). I also have a query builder that generates Linq expressions used to query products. A sample filter would let the user find active/inactive products, generating a Linq expression like: Expression<Func<...

Lambda expression - add to collection if not present

Hi, I have just started learning lambda expressions. Is it possible to simplify the following code down further: Customer customer = Customers.FirstOrDefault(c => c.ID == 3); if (customer == null) { customer = new Customer() { FirstName = "Ben", LastName = "Foster", ID = 3 }; Customers.A...

When is it better practice to explicitly use the delegate keyword instead of a lambda?

Is there any best practice with respect to coding style with respect to explicit use of the delegate keyword instead of using a lambda? e.g. new Thread(() => { // work item 1 // work item 2 }).Start(); new Thread(delegate() { // work item 1 // work item 2 }).Start(); I think the lambda looks better. If the lambda is better style...

returning a lambda function in clisp, then evaluating it

Suppose I have this wonderful function foo [92]> (defun foo () (lambda() 42)) FOO [93]> (foo) #<FUNCTION :LAMBDA NIL 42> [94]> Now, suppose I want to actually use foo and return 42. How do I do that? I've been scrounging around google and I can't seem to come up with the correct syntax. ...

C# lambda expression reverse direction <=

I have seen some code which uses the <= operator. Can you explain what is the use of having lambda in reverse direction? ...

C# (.NET 3.5) Is there any way to get this function name?

I have a function that wraps a call to one of my socket types. If there is an error, I want to be able to print a warning and retry. In the warning, I want to have the method name. However, it was declared as a lambda. Is this even possible? How I call the function (assume in function called myMain): SafeSocketCommand(() => this.mySock...

Combining multiple expressions (Expression<Func<T,bool>>) not working with variables. Why?

ok guys, bare with me. I'll summarize first, then go into detail. I've written a number of methods (.WhereOr, .WhereAnd) which basically allow me to "stack up" a bunch of lambda queries, and then apply them to a collection. For example, the usage with datasets would be a little like this (although it works with any class by using generi...

How to re-write this inner join subquery from SQL to Lambda

SELECT ulcch.ID, ulcch.UserLoginHistoryID, ulcch.StatusID, ulcch.ClientModuleID, ulcch.DeviceState, ulcch.UpdatedAt, ulcch.CreatedAt FROM UserLoginClientConnectionHistory AS ulcch INNER JOIN (SELECT MAX(CreatedAt) AS maxCreatedAt FROM UserLoginClientConnectionHistory AS ulcch1 GROUP BY UserLoginHistory...

How can I make this lambda work?

I have this code: String temp = txtForm.Rtf; foreach (ReplaceStrut rs in replaceArray) { temp = temp.Replace(rs.getNeedle(), rs.getReplacement()); } if (this.InvokeRequired) { this.Invoke(temp => txtForm.Rtf = temp); } else { txtForm.Rtf = temp; } But...

Getting the result from an Expression

I've created a lambda expression at runtime, and want to evaluate it - how do I do that? I just want to run the expression by itself, not against any collection or other values. At this stage, once it's created, I can see that it is of type Expression<Func<bool>>, with a value of {() => "MyValue".StartsWith("MyV")}. I thought at that ...

Defining event handlers

I can define an event like this(declared function): MyElement.Keyup +=MyDeclaredFunction I can also define it like this(anonymous delegate): MyElement.Keyup+=new delegate(object sender, eventargs e) {}; I can also define it like this(lambda): MyElement.Keyup += (sender, e) => myfunction What is the best way to do this? One case...

Generic Database Linq

Given a function as below, i can take a single table from my database and write a lambda using the Where extension method and pretty much build all the other cases using a simple wrapper method and supplying a filter. public void getPeople(Expression<Func<tblPeople, bool>> filter, Action<List<tblPeople>> callback) { ...

Limit collection by enum using lambda

I have a collection of objects. One of the properties is "Type" which is an enum. I want to limit the collection by "type" using a lambda and haven't quite figured out how to do it. Ideas? ...

How to make a list of arrays, not their symbols, in Lisp?

I'm trying to make a function to get a delta between arrays, but right now just want to make a subset: get Nth element. (defvar p1 #(1 2)) (defvar p2 #(3 4)) (mapcar '(lambda (x) (aref x 0)) '(p1 p2)) debugger invoked on a TYPE-ERROR in ... The value P1 is not of type ARRAY. The same error if I make it with make-array. How do...

How Can I Assign an Expression To a Model Property Then Use It In a Partial For an ActionLInk?

Is it possible to assign an Lambda Expression (action?) to a property of my View Model, then use that expression in a Partial? What should the model's property type be? Psuedo Code: View Model public class MyModel { public ????? MyAction {get;set;} } Controller public ActionResult Index() { var model = new MyModel(); model...

Deleting key/value from list of dictionaries using lambda and map

I have a list of dictionaries that have the same keys within eg: [{k1:'foo', k2:'bar', k3...k4....}, {k1:'foo2', k2:'bar2', k3...k4....}, ....] I'm trying to delete k1 from all dictionaries within the list. I tried map(lambda x: del x['k1'], list) but that gave me a syntax error. Where have I gone wrong? ...

Corner case in using lambdas expression in base constructor

In the Framework we are building we need the following pattern: public class BaseRenderer { Func<string> renderer; public BaseRenderer(Func<string> renderer) { this.renderer = renderer; } public string Render() { return renderer(); } } public class NameRenderer : BaseRenderer { public st...

Setting numpy slice in lambda function

I want to create a lambda function that takes two numpy arrays and sets a slice of the first to the second and returns the newly set numpy array. Considering you can't assign things in lambda functions is there a way to do something similar to this? The context of this is that I want to set the centre of a zeros array to another array ...