lambda

Call constructor as a function in C#

Is there a way in C# to reference a class constructor as a standard function? The reason is that Visual Studio complains about modifying functions with lambdas in them, and often its a simple select statement. For example var ItemColors = selectedColors.Select (x => new SolidColorBrush(x));, where selectedColors is just an IEnumerable<...

How to get the instance of a reffered instance from a lambda expression

I have this lambda expression Expression<Func<bool>> commandToExecute Then I pass an instance of a class in there with a method: _commandExecuter.ProcessCommand (() => aClass.Method()) How do I get the instance of aClass within the ProcessCommand method? I want to execute some addiontal methods of this class or get some property va...

method subscription vs lambda delegate subscription - Which and why?

I have seen some people leaning on handing methods to callbacks/events and then sometimes just handing them lambdas. Can anybody speak to any difference between the two? I would have originally thought them to be the same, but the inconsistency I have seen implemented sometimes makes me wonder if there is a case where one is preferable ...

Linq Expression string with inline values

I am trying to build expression strings for use with IOC extended nHydrate. When I call ToString() on my expression I get something like this: employee => employee.Username == value( Some type name here ) Is there a way to resolve that value call to the actual value? employee => employee.Username == "Captain Spiffy" ...

Expression generated based on interface

I'm geting the exception Unable to cast the type 'MySomeTypeThatImplementsISomeInterfaceAndIsPassedAs[T]ToTheClass' to type 'ISomeInterface'. LINQ to Entities only supports casting Entity Data Model primitive types. my repository looks like public interface IRepository<T> { IQueryable<T> Get(System.Linq.Expressions.Expression<Syste...

Wrappers around lambda expressions

I have functions in python that take two inputs, do some manipulations, and return two outputs. I would like to rearrange the output arguments, so I wrote a wrapper function around the original function that creates a new function with the new output order def rotate(f): h = lambda x,y: -f(x,y)[1], f(x,y)[0] return h f = lambda...

create a generic method that sorts List<T> collections

What I used to do is create a case switch that sorts my LINQ in this format: List<products> productList = GetAllLists(); switch (sortBy) { case "name": return productsList.OrderBy(pl => pl.Name); case "date": return productsList.OrderBy(pl => pl.DateCreate); } which, in the long run, becomes cumbersome. I wanted to hav...

Like in Lambda Expression and LINQ

Simple Question how to have something like this customers.where(c=>c.Name **like** "john"); i know this isn not possible but i was wondering how can i have something similar. thanks in advance. ...

Need help understanding lambda (currying)

i am reading Accelerated C# i don't really understand the following code: public static Func<TArg1, TResult> Bind2nd<TArg1, TArg2, TResult> ( this Func<TArg1, TArg2, TResult> func, TArg2 constant ) { return (x) => func( x, constant ); } in the last line what is x referring to? and there's another: public static Func<TAr...

IntMap changes type after innocent mapping

consider this piece of code:Welcome to Scala version 2.8.0.r0-b20100714201327 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20). scala> val a = IntMap((1,1)) a: scala.collection.immutable.IntMap[Int] = IntMap((1,1)) scala> a.map(x => (x._1,x._2 + 1)) res23: scala.collection.immutable.Map[Int,Int] = Map((1,2)) header of IntMap.map says t...

Generic extension method : Type argument cannot be inferred from the usage

I'm trying to create a generic extension method, that works on typed data tables : public static class Extensions { public static TableType DoSomething<TableType, RowType>(this TableType table, param Expression<Func<RowType, bool>>[] predicates) where TableType : TypedTableBase<RowType> where RowType : DataRow { ...

Passing A Lambda into a method with MVCContrib

I have a method in my class that takes a bool and returns a string so that the user doesn't see true or false in the view. I am then trying to use this method in the MVCcontrib grid public string BidStatusText(bool status) { if (status == true) return "Bid Confirmed"; else if (status == false) ...

LINQ Query to match a sequence

Hi, I have a result set, say clientList. And I want to make sure that ALL the clients in the ClientList have a Profession as "Banker". Let the result of the query be a boolean value. How can I write the LINQ or lambda expression query to get the result? ...

Find duplicates in List

I have the following code: List<MyType> myList = new List<MyType>(); // ... add items to the list var dupes = myList.GroupBy(g => g).Where(x => (x.Count() > 1)) .Select(x => new { obj = x.Key, count = x.Count() }).ToList(); dupe is always empty, even if I intentionally insert duplicates into the list. What should I a...

MVC LINQ Query question

In my public ActionResult Active() { var list = _entity.CALL_UP.Where(s => s.STATE == ICallUpsState.FULLY_SIGNED) .Where(f => f.START_DATE <= DateTime.Today && f.END_DATE >= DateTime.Today) .ToList(); return View(list); } This r...

Issue while building dynamic Expression Tree

Hi, I am trying to build a dynamic Expression Tree like below : Func<IEnumerable<int>, int, bool> dividesectionmethod = (x, y) => { int nos1 = 0; int nos2 = 0; foreach (int i in x) { if (i <= y) nos1++; ...

Why can't I display data in DataGrid after executing Lambda Expression?

I have a data table. If I set the DataSource of a DataGrid to the data table it displays all of the data just fine. However if I use a lambda expression to filter out some of the data and then reassign the datasource it does not display anything. This is what I have tried... var AllPeople = from r in CompanyDataTable.AsEnumerable()...

Is using Lambda expression for try without catch a bad practice?

I have many portions in code where I do try { // not so important code, but don't want this to affect anything else } catch { } Is using this considered as a bad practice? Try(() => { }); void Try(Action a) { try { a(); } catch { // ignore. } } ...

How to convert Expression<Func<T, TProperty>> to Expression<Func<T, TNewProperty>>

The following is working, but my Body.NodeType changes to Convert, instead of MemberAccess which is a problem when getting ModelMetadata.FromLambdaExpression: private Expression<Func<TModel, TNewProperty>> ConvertExpression<TProperty, TNewProperty>(Expression<Func<TModel, TProperty>> expression) { Exression converted = Expression.Co...

minimum value in dictionary using linq

Hi I have a dictionary of type Dictionary<DateTime,double> dictionary How can I retrive a minimum value and key coresponding to this value from this dictionary using linq ? ...