lambda

Create properties using lambda getter and setter

I have smth like this: class X(): def __init__(self): self.__name = None def _process_value(self, value): # do smth pass def get_name(self): return self.__name def set_name(self, value): self.__name = self._process_value(value) name = property(get_name, set_name) Can I re...

Retrieving the name of the the invoked method executed in a Func

I would like to get the name of the method that is being delegated as a Func. Func<MyObject, object> func = x => x.DoSomeMethod(); string name = ExtractMethodName(func); // should equal "DoSomeMethod" How can I achieve this? -- For bragging rights -- Make ExtractMethodName also work with a property invocation, having it return the p...

How to filter child collections in Linq.

Hi! I need to filter the child elements of an entity in linq using a single linq query. Is this possible? Suppose I have two related tables. Verses and VerseTranslations. The entity created by LINQ to SQL is such that i have a Verse object that contains a child object that is a collection of VerseTranslation. Now if i have the follow...

No Multiline Lambda in Python: Why not?

I've heard it said that multiline lambdas can't be added in Python because they would clash syntactically with the other syntax constructs in Python. I was thinking about this on the bus today and realized I couldn't think of a single Python construct that multiline lambdas clash with. Given that I know the language pretty well, this s...

Using a single Func<T,bool> with Where() and inheritance

I'm trying to use a single Func<T,bool> definition to handle a class and its inheritor. This is what I have: Func<Job, bool> ValidJob = j => !j.Deleted && !j.OnHold && j.PostDate <= DateTime.Now && j.ExpireDate > DateTime.Now; public class JobExtended : Job { } So, given that, the following works: IQueryable<Job> jobs = ......

Is this possible with LINQ-to-Entities query using Lambda Expression

Looking for direction and understanding of the approach to implement logic to handle the scenario below, if possible with LINQ-to-Entities. New to LINQ and Entity Framework, but understand the basics. Intermediate with C#. I have a function, which needs to iterate through and add information to a results set and ultimately pass back t...

Lambda Expression for Class Property

Can someone explain why the first of the two following examples is valid and the other is not? More specifically, how is a relationship created between T and TProperty in the first example? //Example 1 class SomeClass<T> where T : class { void SomeMethod<TProperty>( Expression<Func<T,TProperty>> expression ){ ... } } //Example 2...

Test for List<T> membership using a List<T>

Does anyone know if there is a way to test for list membership utilizing a list. For example I have a class named Membership which has a property Rebates which is of type List<Enums.RebateType>. I want to test using a lambda expression to see if that list contains any rebates that are of a specific type. My orginal lambda expression is a...

c#, lambda expresions, where is the error?

I have this method public static List<Contact> Load(string filename) { if (!File.Exists(filename)) { throw new FileNotFoundException("Data file could not be found", filename); } var contacts = System.Xml.Linq.XDocument.Load(filename).Root.Elements("Contact").Select ...

SubSonic 3.0 Create New and Delete and Insert Pattern Problem

3003 is the exact version I think. Anyway, I have the below code (using ActiveRecord) for creating 2 new entities, deleting any that have the same "Stamp1" and "Stamp2" values, and then I want to insert everything in my List. I cannot tell if the code is efficient for the delete, and I have no idea how to insert everything new from a lis...

How do predicate parameters work syntactically in C# ?

Some Object Relational Mapping (ORM) frameworks (such as LLBLGen) allow you to specify "predicate" parameters to the query methods, such as (simplified a bit): var entities = adapter.FetchEntities(EntityType.Employee, EmployeeFields.Salary > 50000); How does that 2nd parameter work syntactically in C#? It kind of looks like a lam...

How do I loop through a regex's matches inside a replace in javascript?

I have the following JavaScript (the spaces in the <P>s are non-breaking): var html = '...<li>sub 2</li></ol></li></ol>\n\ <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n...

boost::lambda::if_then for copy_if

Hello experts I have been trying to emulate a copy_if by the following codes but my compiler (g++-4.0) just keeps complaining. What's technically wrong? Thanks for your help! template <class STL> // a std container of class A, but I don't know if it's a list or vector or deque void export_(STL& Alist) { //a member function for_each(Al...

Is using delegates excessively a bad idea for performance?

Consider the following code: if (IsDebuggingEnabled) { instance.Log(GetDetailedDebugInfo()); } GetDetailedDebugInfo() may be an expensive method, so we only want to call it if we're running in debug mode. Now, the cleaner alternative is to code something like this: instance.Log(() => GetDetailedDebugInfo()); Where .Log() is d...

How to combine two expressions: result = exp1(exp2);

As subject, how to combine two expressions into a single one for this case: Expression<Func<IEnumerable<T>, IEnumerable<T>>> exp1; Expression<Func<IEnumerable<T>, IEnumerable<T>>> exp2; Expression<Func<IEnumerable<T>, IEnumerable<T>>> result = ???; // exp1(exp2) ...

C# Expression using And Or and Not expression together based on AST

I want to use Linq expression for some dynamic features. I need And, Or and Not expressions.. I couldn't get much.. We want to check whether certain feature has been enabled or not in our system and based on that we will decide whether to show the menu item or not. we have formed rules in XML format, I know to convert the rule to AST bu...

How to write an empty list using S, K and I combinators?

I know that: (cons [p] [q]) is ((s ((s i) (k [p]))) (k [q])) (car [lst]) is ([lst] k) (cdr [lst]) is ([lst] (k i)) I want to write a list like this (cons [a] (cons [b] (cons [c] [nil]))) , which is going to be something like this: ((s ((s i) (k [a]))) (k ((s ((s i) (k [b]))) (k ((s ((s i) (k [c]))) (k [nil])))))) But I don't kno...

.Net anonymous methods ... where is the miracle ?

This function returns 1210 but not 385, why ? public int CalcSquaresSum() { int sumOfSquares = 0; List<Func<int>> functions = new List<Func<int>>(); for (int i = 1; i <= 10; i++) { functions.Add(() => i * i); } foreach (var function in functions) { sumOfSquares += function(); // why function() is al...

Lambda expression to grab all values inside a Dictionary

I have a LINQ query which returns all the values inside a Dictionary, conditional upon something: var apps = from entry in shape.Decorators where entry.Value == DecoratorLayoutStyle.App select entry.Key; shape.Decorators is a Dictionary<Shape, DecoratorLayoutStyle> Is there something terser, and/or can I use a combination of lambd...

Distinct() with lambda?

Right, so I have an enumerable and wish to get distinct values from it. Using System.Linq, there's of course an extension method called Distinct. In the simple case, it can be used with no parameters, like: var distinctValues = myStringList.Distinct(); Well and good, but if I have an enumerable of objects for which I need to specify ...