lambda

Default parameter values in C#'s lambda expressions

Good afternoon, Can someone please tell me if I can set default parameter values when using lambda expressions in C#? For example, if I have the code public static Func<String, Int32, IEnumerable<String>> SomeFunction = (StrTmp, IntTmp) => { ... }, how can I set IntTmp's default value to, for example, two? The usual way to set defaul...

How can I convert a Predicate<T> to an Expression<Predicate<T>> to use with Moq?

Please help this Linq newbie! I'm creating a list inside my class under test, and I would like to use Moq to check the results. I can easily put together a Predicate which checks the results of the list. How do I then make that Predicate into an Expression? var myList = new List<int> {1, 2, 3}; Predicate<List<int>> myPredicate = (lis...

C++ lambda operator ==

How do I compare two lambda functions in C++ (Visual Studio 2010)? std::function<void ()> lambda1 = []() {}; std::function<void ()> lambda2 = []() {}; bool eq1 = (lambda1 == lambda1); bool eq2 = (lambda1 != lambda2); I get a compilation error claiming that operator == is inaccessible. EDIT: I'm trying to compare the function instance...

C# Lambda Expression Mapping Multiple Conditions

I am using Enterprise Library.I want to map the column (of integer type) to Enum Type. Say Enum BloodGroup Type { OPositive, ONegative, ABPositive, ABNegative, BPositive, BNegative, NotSet } I am mapping Database Table's column to C# Types's (Class Employee) Properties. IRowMapper<Employee> addressMapp...

Can I capture a local variable into a LINQ Expression as a constant rather than a closure reference?

I'd like to say int x = magic(), y = moremagic(); return i => i + (x/y); and have the x be captured as a constant instead of a variable reference. The idea is that x will never change and so when the expression is later compiled, the compiler to can do constant folding and produce more efficient code -- i.e. calculating x/y once inst...

LambdaExpression CompileToMethod

I Have a few lines of code public void CreateMethod<TContract>(Expression<Action<TContract>> method) { var innerMethod = Builder.DefineMethod("SomeName",MethodAttributes.Private); method.CompileToMethod(innerMethod); //more code } However the second line fails. I've tried with different versions of DefineMethod with little lu...

Why does adding a return type prevent me from using method group syntax?

I'm trying to use a method group in a lambda expression, like this: public class Foo { public void Hello(string s) { } } void Test() { // this works as long as Hello has a void return type Func<Foo, Action<string>> a = foo => foo.Hello; } When I change the return type of Hello to int, however, I get 'Bar.Hello(string)' ha...

Create expression with different types

How can I change if (propType.PropertyType == typeof(string)) and if (propType.PropertyType == typeof(int)) to something more dynamic? private void button2_Click(object sender, EventArgs e) { var lista = _pessoas.AsQueryable(); if (textBox2.Text != "") { var param = Expression...

manually build linq expression for x => x.Child == itemToCompare.Child

We have an object and we want to build a linq query based on that object on the fly. This linq statement is equivalent to what we want to build: Expression<Func<Sample, bool>> linqExpression = x => x.Child == itemToCompare.Child; We can't quite come up with the right expression to build the itemToCompare.Child part. Here'...

Dictionary Manipulation Using LINQ in C#

I'm having a Dictionary like Dictionary<String, List<String>> MyDict = new Dictionary<string, List<string>> { {"One",new List<String>{"A","B","C"}},{"Two",new List<String>{"A","C","D"}} }; I need to get a List<String> from this dictionary, The List should contain Distinct items from the values of the above ...

C# : How to add data data from a ListView control to a Dictionary.

The list view is like i need to add the data from the Listview to a Dictionary<String,String>. Now i'm using for loop to do this. Is there any way to do this using LINQ. Finally the Dictionary will contain {"A","1"} {"B","2"} {"C","3"} EDIT My code : Dictionary<String, String> Dic = new Dictionary<string, string>(); ...

C#:Conversion of Dictionary<String,String> to Dictionary<String, Dictionary<String,String>>

The First Dictionary is like Dictionary<String, String> ParentDict = new Dictionary<String, String>(); ParentDict.Add("A_1", "1"); ParentDict.Add("A_2", "2"); ParentDict.Add("B_1", "3"); ParentDict.Add("B_2", "4"); ParentDict.Add("C_1", "5"); i need to convert this into a new Dictionary...

How to create a Generic lambda expression. Many of my entities perform the same Lambda expression predicate.

I use Entity Framework 4. How can I perform a Generic Where Lambda Clause.? I Have many Entity that need the same Where Query. public Func<SupplierTypeText, bool> GetLmbLang() { return (p => p.LangID == 1); } public Func<ProductText, bool> GetLmbLang() { return (p => p.LangID == 1); } public Func<CategoryText, bool> GetLmbL...

Is _1 part of C++0x?

I've seen two recent answers using _1 as a pure C++0x solution (no explicit mention of boost lambdas). Is there such an animal as std::_1 I would think that having native lambdas will make such a construct redundant. A Google code search for std::_1 brings two results from the same project so that's inconclusive. ...

Any suggest to store a value in a lambda expression

Hi, I'm trying to write an in-line function for count occurrences of a word in a string using lambda expressions recursively. The function: Func<string, string, int> getOccurrences = null; getOccurrences = (text, searchTerm) => text.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) == -1 ? 0 : getOccurrences( text.Su...

How do I perform an assignment operation in a lambda expression

I have a list of items and wish to set one of their properties to a certain value: applist.ForEach(Function(x) x.PrePaidTypeID = CInt(DBEnums.PrePaidType.NoPrepay)) ...but we think this just does a boolean comparison. Is there a way to force VB to assign the integer value rather than compare it? ...

why does (sender,e) => SomeAction() works on winforms and not in asp.net

Hello, I have the following code: btnTest.Click += (sender,e) => SomeAction() why does this code works in WinForms and not in asp.net. In asp.net I had to do the following: btnTest.Click += new EventHandler(SomeAction); target framework in both cases is .net 4.0 ...

Count in lambda expression

I am trying to run a query where I get the name of locations and the number of items in that location. So if i have a program that contains 3 locations I want to know how many programs are in that location..I need to use this with a lambda expression or linq to entities. return Repository.Find(x => x.Location.Name.Count())...clearly mis...

3 entities execute the same lambda predicate. How creating one lambda expression generic for these entities.

I Have 3 entities. Product LangID ProductName Category LangID CatName ProductType LangID TypeName As you can see, each of them has LangID Property. I Would like be able to create a generic repository that will contain only one function that will return an Func<T, bool> GetLmbLang() public interface IBaseRepository<T> where T : c...

Why does list initialization with lambda causes high cyclomatic complexity?

Initialization of list with lambdas causes high IL cyclomatic complexity: why, and how remove this complexity? For example following code causes the static constructor of the class (which is actually compiler generated) to be very complex: 1 + the list count. static List<Predicate<string>> list = new List<Predicate<string>>() { s =>...