lambda

Implicit operators, Linq and Lambda expressions can make code less readable. But what is more readable?

In answering this question http://stackoverflow.com/questions/808725/why-does-a-linq-castt-operation-fail-when-i-have-an-implicit-cast-defined I have found that there are a number of ways to implicitly cast between objects. Consider the following two classes: public class Class1 { public int Test1; } public class Class2 { pu...

Refactoring with Lambda's and Delegates

I've just installed VS2008 and have run into a problem that I'm sure can be solved with either lambda's or delegates (or a combination!). private string ReadData(TcpClient s, string terminator) { // Reads a byte steam into a string builder until either data is unavailable or the terminator has not been reached va...

Passing a parameter using RelayCommand defined in the ViewModel (from Josh Smith example)

I would like to pass a parameter defined in the XAML (View) of my application to the ViewModel class by using the RelayCommand. I followed Josh Smith's excellent article on MVVM and have implemented the following. XAML Code <Button Command="{Binding Path=ACommandWithAParameter}" CommandParameter="Orange" ...

How to do multiline lambda expressions in f#?

How would I do this (C#) in F# public class MyClass { void Render(TextWriter textWriter) { Tag(() => { textWriter.WriteLine("line 1"); textWriter.WriteLine("line 2"); }); Tag(value => { textWriter.WriteLine...

List<object>.Contains Expression Tree

I would like to build an Expression that would equate to expected... Expression<Func<ReferencedEntity, bool>> expected = (ReferencedEntity referencedEntity) => foreignKeys.Contains(referencedEntity.Id); Expression<Func<ReferencedEntity, bool>> actual; foreignKeys type is a List<object> Here is what I have so far and I think it would ...

Unit Testing Expression Trees

I recently need to build a Expression tree so I wrote a Test method like so... /// <summary> /// /// </summary> [TestMethod()] [DeploymentItem("WATrust.Shared.Infrastructure.dll")] public void BuildForeignKeysContainsPredicate_shoud_build_contains_predicate() { RemoteEntityRefLoader_Accessor<ReferencedEntity> target = CreateRe...

C# String to Expression Tree

This is a simplified version of the original problem. I have a class called Person: public class Person { public string Name { get; set; } public int Age { get; set; } public int Weight { get; set; } public DateTime FavouriteDay { get; set; } } ...and lets say an instance: var bob = new Person(){ Name = "Bob", ...

How to get value of a Lambda MemberExpression

Given a Lambda Expression: Define(Expression<Func<T, int>> property) and used like: Define(x => x.Collection.Count) What is the best method of getting the value of Count? Is there an easy way with the Expression Tree or should I use reflection to parse the tree to get the PropertyInfo and GetValue()? ...

Writing Extension methods with Action / Func / Delegates / Lambda in VB and C#

hey guys. Firstly I can't get my head around the functional / Lambda aspects of .NET 3.5. I use these features everyday in LINQ, but my problem is understanding the implementation, and what they really mean (Lambda? System.Func? etc etc) With this in mind, how would the following be achieved: As an example, I'd like to have an Extensi...

ASP.NET BeginForm() expression syntax

Hi, I'd like to use the expression-based syntax for ASP.NET MVC's Html.BeginForm (e.g. Html.BeginForm<HomeController>(a => a.ActionForSubmit();) for the increased testability it gives you. I'm unclear about what to do where the corresponding action has parameters. For example, I have a login action that is HTTP POST only and has two pa...

PHP sandbox/sanitize code passed to create_function

Hello, I am using create_function to run some user-code at server end. I am looking for any of these two: Is there a way to sanitize the code passed to it to prevent something harmful from executing? Alternately, is there a way to specify this code to be run in a sandboxed environment so that the user can't play around with anything ...

how do I use STL algorithms with a vector of pointers

I have a vector of pointers that are not owned by the container. How do I use algorithms on the targets of the pointers. I tried to use boost's ptr_vector, but it tries to delete the pointers when it goes out of scope. Here is some code that needs to work: vector<int*> myValues; // ... myValues is populated bool consistent = count(my...

Universal method with lambda’s criteria at Business Objects Collection

I have abstract class for each Business Object in my app (BO) Also have parent (generic) collection to store all BOs (named BOC - Business Object Collection) What I would like to archive is created universal method iniside BOC similar to: public T GetBusinessObject (lambda_criteria) So I could call this method similar to: Employe...

linq query to join two tables and get the count from one table values from the other

I have two tables Customers, Orders Customers CustomerID FName LName Orders OrderId CustomerID OrderDate I want to make a linq statement that can join these two tables and get FName, LName, Count of orders for each customer ...

How can I get a value from a CheckBoxColumn using C# 3.0?

I can get the current selected row in this way: private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e){ //Cells[0] cause CheckBoxColumn is in that index (first column) DataGridViewCheckBoxCell temp = (DataGridViewCheckBoxCell)dgv.Rows[e.RowIndex].Cells[0]; } So, Now I want to get all of the rows that...

Lambda Example

I am still learning some of the features of C# 3.0 and want to know if the following can be reduced to a lambda expression. var SomeObject = Combo.EditValue; var ObjectProperty = SomeObject.Property; To obtain ObjectProperty from the combo.editvalue in a single line? Also, if you can provide me with any good references to Lambda expr...

Threading and lambda expressions

What is the difference between the two piecees of code below. will there be any issues using the second one. Scenario 1 private void Log(Exception e) { ThreadPool.QueueUserWorkItem(new WaitCallback(Log), e); } private void Log(object obj) { Exception e = (Exception)obj; Logger.Log(e); } Scenario 2 private void Log(Excep...

What's going on with the lambda expression in this python function?

Why does this attempt at creating a list of curried functions not work? def p(x, num): print x, num def test(): a = [] for i in range(10): a.append(lambda x: p (i, x)) return a >>> myList = test() >>> test[0]('test') 9 test >>> test[5]('test') 9 test >>> test[9]('test') 9 test What's going on here? A functi...

convert a string[] to string with out using foreach

string x; foreach(var item in collection) { x += item+","; } can I write something like this with lambdas? ...

How do I dynamically create an Expression<Func<MyClass, bool>> predicate?

Hi all, How would I go about using an Expression Tree to dynamically create a predicate that looks something like... (p.Length== 5) && (p.SomeOtherProperty == "hello") So that I can stick the predicate into a lambda expression like so... q.Where(myDynamicExpression)... I just need to be pointed in the right direction. Thanks. Ed...