lambda

Help with linq2sql generic lambda expression

In my Database almost every table has its own translations table. I.e. Sports has SportsTranslations table with columns: SportId, LanguageId, Name. At the moment I'm taking translations like: int[] defaultLanguages = { 1, 3 }; var query = from s in dc.Sports select new { sportName = s...

Odd Lambda Expression Question

List<string> a = new List<string>() { "a", "b", "c" }; List<string> b = new List<string>() { "a", "b", "c", "d", "e", "f" }; b.RemoveAll(a.Contains); If you loop through b it will now only contain d e and f. Can anyone expand out whats actually happening, because currently it doesnt make any sense at all. Edit: I was more talking abo...

How to write this query as lambda expression?

I'm still having problems to write lambda expressions that should create some object and to set properties with the object initializer. How would this query be written as a lambda expression? List<CategoryContainer> _catList = (from q in _dc.Category select new CategoryContainer ...

Replacing a regular method with an anonymous method in C#/LINQ

I have a LINQ query that looks like this: public IEnumerable<Foo> SelectFooBars() { return from f in foos join b in bars on f.BarId equals b.Id select AddMissingProp(f, b.MissingProp); } public void AddMissingProp(Foo foo, string missingProp) // substitute this...

How to deep copy an object containing a lambda expression?

It's me again, about my Mega Man game. I switched over to a component system so that objects could be data driven. It's going ok but I've hit a problem. My objects have states, specified with an input file. Those states have triggers to transition them to other states. The conditions for a state change are also in the input file, and ar...

Cannot use 'this' in member initializer?

Is this legal? Does it contain a hidden bug or flaw? Visual studio does not give any errors or warnings but ReSharper does: /// <summary> /// immutable tuple for two /// </summary> public class Pair<TValue1, TValue2> : Singleton<TValue1> { public TValue2 Value2 { get; private set; } public Pair(TValue1 value1, TValue2 value2, Fu...

Lambda, calling itself into the lambda definition

I'm doing a complicated hack in Python, it's a problem when you mix for+lambda+*args (don't do this at home kids), the boring details can be omited, the unique solution I found to resolve the problem is to pass the lambda object into the self lambda in this way: for ... lambda x=x, *y: foo(x, y, <selflambda>) It's possible?, thank...

Creating function pointers to functions created at runtime

Hello, I would like to do something like: for(int i=0;i<10;i++) addresses[i] = & function(){ callSomeFunction(i) }; Basically, having an array of addresses of functions with behaviours related to a list of numbers. If it's possible with external classes like Boost.Lambda is ok. Edit: after some discussion I've come to conclusion...

Can a C# expression ever return void?

I have the following method, and I want to know if there is anything that can go in place default(void) below because there is a compiler error that says that void is not valid here: private void applyDefaultsIfNecessary(ApplicationConfiguration configuration) { var defaults = new Dictionary<Predicate<ApplicationConfiguration>, Acti...

Problems casting objects of type Func<T,T> in C#

Hi there, Why doesn't this compile? I suppose there are ways to work around this; just curious. Thanks! static void Main(string[] args) { Func<int, int> f_int = n => n * n; Func<int, double> f_double = (Func<int, double>)f_int; } ...

More on casting Func<T,T> and Expression<Func<T,T>>

Hi there, I've spent hours with this but haven't managed... Please see example below - How can this be done? The idea is to build a compiled expression of type Func<dynamic, dynamic> given an Expression<Func<T1,T2>> passed by the class' consumer. I have been able to solve this problem (thanks to SO) IF the types T1 and T2 are known at...

.NET 4.0: What does Expression.Reduce() do?

Hi again, I've been working with expression trees for a few days now and I'm curious to know what Expression.Reduce() does. The msdn documentation is not very helpful as it only states that it "reduces" the expression. Just in case, I tried an example (see below) to check if this method included mathematical reduction, but this doesn't ...

How to use boost lambda to populate a vector of pointers with new objects

Hi, I've recently started using boost lambda and thought I'd try and use it in places where it will/should make things easier to read. I have some code similar to the following std::vector< X * > v; for ( int i = 0 ; i < 20 ; ++i ) v.push_back( new X() ); and later on, to delete it... std::for_each( v.begin(), v.end(), boost::l...

Create LINQ query at runtime to GroupBy in EntityFramework (with inheritance).

This is the scenario. I have the following three classes, they are defined in Entity Framework, i only define them here for the example: public class Foo { public string Color { get; set; } } public class Bar : Foo { public string Height { get; set; } } public class Pipe : Foo { public string Width { get; set; } } So, I have m...

Cannot convert expression type 'lambda expression' to return type 'System.Linq.Expressions.Expression<System.Func<IProduct,string,bool>>'

Ok, I'm lost. Why is the 1st function WRONG (squiglies in the lamda expression), but the 2nd one is RIGHT (meaning it compiles)? public static Expression<Func<IProduct, string, bool>> IsValidExpression(string val) { return (h => h.product_name == val); } public static Expression<Func<IProduct, bool>> IsValidEx...

.NET 4.0: How to create an Expression<Func<dynamic, dynamic>> - Or is it a bug?

Hi All, During my work with expression trees for a few days now, I came across something that I find difficult to understand; hopefully someone will be able so shed some light here. If you code Expression<Func<dynamic, dynamic>> expr1 = x => 2 * x; the compiler will complain and you won't get anywhere. However, it seems that if you cre...

when should i use lambda expressions which comes with C# 3.0?

Hai guys, My fellow developers were talking about lambda expressions this morning. So i decided to ask it here in SO when should i use lambda expression which comes with C# 3.0? ...

Order IList with field name in argument ...

Hello, I have this simple piece of code public ActionResult ListToGrid(string field, string direction) { _model.MyList = _repo.List(); } To sort, I can do this : _model.MyList = _employeeService.List().OrderBy(x => x.FirstName).ToList<Employee>(); But I'd like use "as field" the name receive (field) in argument and the directi...

C#: Is it possible to check that a simple expression will always return true?

And I mean this in the easiest way. Say you have a function with the following signature: public static Expression<Func<T, bool>> CreateExpression<T>(string value) { // ... } Usually it will create a more complex expression of some sort, but if the value is null the method should return a constant, always true expression. In other...

How to pass action with two parameters using Lambda expression to method?

I have a class that takes an action in it's constructor. Example: public CustomClass(Action<Path> insert) { // logic here... } I currently instantiate this class using the following line of code: var custom = new CustomClass((o) => LayoutRoot.Children.Add(o)); I want to modify the custom class to include an additional construc...