lambda

other way to add item to List<>

In my other question You can see code of my arr structure and PriorityQueue collection. I normally add items to this collection like that: arr.PriorityQueue.Add(new element((value(item, endPoint) + value(startPoint, item)),item)); I am curious that is other way to do this (add element(which is struct) object to List) ? In lambda way f...

Wrap Sub as Function for use in Lambda

I have a problem with VB9 and Moq. I need to call a verify on a Sub. Like so: logger.Verify(Function(x) x.Log, Times.AtLeastOnce) And my logger looks like this: Public Interface ILogger Sub Log() End Interface But with VB this is not possible, because the Log method is a Sub, and thereby does not produce a value. I don't want...

How to create a Linq To Entities expression

HI, I'm using Linq To Entities and I'd like to convert this return db.Products .Where(p => p.idUser.Equals(id) && p.Category.Genre.Any(g => g.visible)) into something like Func<Genre, bool> expr = g => g.visible return db.Products .Where(p => p.idUser.Equals(id) && p.Cate...

Executing a modified expression

I found this brief demo: http://msdn.microsoft.com/en-us/library/bb546136.aspx Which discusses modifying an expression. However the code starts with a Expression<Func<string, bool>> and ends up with a Expression so it's not complete. How do I take that expression and make it typed as Expression<Func<string,bool>> again? All the exampl...

Linq-to-SQL: Ignore null parameters from WHERE clause

The query below should return records that either have a matching Id supplied in ownerGroupIds or that match ownerUserId. However is ownerUserId is null, I want this part of the query to be ignored. public static int NumberUnderReview(int? ownerUserId, List<int> ownerGroupIds) { return ( from c in db.Contacts ...

How to properly translate the "var" result of a lambda expression to a concrete type?

So I'm trying to learn more about lambda expressions. I read this question on stackoverflow, concurred with the chosen answer, and have attempted to implement the algorithm using a console app in C# using a simple LINQ expression. My question is: how do I translate the "var result" of the lambda expression into a usable object that I c...

Why doesn't this (translated) VB.NET code work?

I had a piece of C# code converted, but the translated code isn't valid... Can somebody help out? C# <table> <% Html.Repeater<Hobby>("Hobbies", "row", "row-alt", (hobby, css) => { %> <tr class="<%= css %>"> <td><%= hobby.Title%></td> </tr> <% }); %> </table> VB <% Html.Repeater(of Hobby)(Model.Hobbies, "row", "row-alt", ...

Good tutorials for lambda

Is there any good link for lambda expression (C#) for learn? If so kindly suggest me one. In google in searched but may be my luck failed to get any suitable one. Thanks ...

How to Process Lambda Expressions Passed as Argument Into Method - C# .NET 3.5

My knowledge of Lambda expressions is a bit shaky, while I can write code that uses Lambda expressions (aka LINQ), I'm trying to write my own method that takes a few arguments that are of type Lambda Expression. Background: I'm trying to write a method that returns a Tree Collection of objects of type TreeItem from literally ANY other o...

How can I use external expressions in Linq with EF4 (and LINQKit)?

I want to separate out often used expressions in linq queries. I'm using Entity Framework 4 and also LINQKit but I still don't know how I should do it the right way. Let me give you an example: Article article = dataContainer.Articles.FirstOrDefault(a => a.Id == id); IEnumerable<Comment> comments = (from c in article.Comments wher...

linq-to-sql combine child expressions

I need to create and combine several expressions for child entity into one to use it on "Any" operator of a parent. Code now looks like this: Expresion<Child, bool> startDateExpression; if(String.IsNullOrEmpty(startDate) startDateExpression = t => true; else startDateExpression = t => t.start_date >= startDate; Expression<Ch...

linq-to-sql combine expressions

Is there any way I can combine list of expressions into one? I have List<Expression<Child, bool>> expList and trying to combine into one (AndAlso) and get Expression<Child, bool> combined = Combine(expList); Intended usage for combined expression is this: //type of linqFilter is IQueryable<Parent> linqFilter = linqFilter.SelectMany...

how do I combine several Action<T> into a single Action<T> in C#?

How do I build an Action action in a loop? to explain (sorry it's so lengthy) I have the following: public interface ISomeInterface { void MethodOne(); void MethodTwo(string folder); } public class SomeFinder : ISomeInterface { // elided } and a class which uses the above: public Map Builder.BuildMap(Action<ISomeInterfa...

How does the proc in the caches_action if clause get execute

I have a newbie kind of question which I cant get my head around. How does the Proc in the if condition of the caches_action get executed for the caches_action method. for example caches_action :show, :if=>Proc.new{|x| something} what i dont get its how does this get called. I know i can execute a proc defined as proc= Proc.new by...

Specializing a template on a lambda in C++0x

I've written a traits class that lets me extract information about the arguments and type of a function or function object in C++0x (tested with gcc 4.5.0). The general case handles function objects: template <typename F> struct function_traits { template <typename R, typename... A> struct _internal { }; template <typename ...

.NET: Best way to execute a lambda on UI thread after a delay?

I had a situation come up that required running a lambda expression on the UI thread after a delay. I thought of several ways to do this and finally settled on this approach Task.Factory.StartNew(() => Thread.Sleep(1000)) .ContinueWith((t) => textBlock.Text="Done",TaskScheduler.FromCurrentSynchronizationContext()); But I'm wonderi...

Need help with this basic Contains<>() extension method and Lambda expressions

Hi, Say I have the following class: class Foo { // ctor etc here public string Bar { get; } } Now, I have a LinkedList of Foos declared like so: LinkedList<Foo> How would I write a basic Contains<>() for this? I want to be able to do this: Foo foo = new Foo(someString); LinkedList<Foo> list = new LinkedLis...

Python lambda returning None instead of empty string

I have the following lambda function: f = lambda x: x == None and '' or x It should return an empty string if it receives None as the argument, or the argument if it's not None. For example: >>> f(4) 4 >>> f(None) >>> If I call f(None) instead of getting an empty string I get None. I printed the type of what the function returned ...

Lambda expression will not compile

I am very confused. I have this lambda expression: tvPatientPrecriptionsEntities.Sort((p1, p2) => p1.MedicationStartDate .Value .CompareTo(p2.MedicationStartDate.Value)); Visual Studio will not compile it and complains about syntax. I converted the lamba expression to an anonymous delegate as so: tvPatientPrecriptio...

Lambdas within Extension methods: Possible memory leak?

I just gave an answer to a quite simple question by using an extension method. But after writing it down i remembered that you can't unsubscribe a lambda from an event handler. So far no big problem. But how does all this behave within an extension method?? Below is my code snipped again. So can anyone enlighten me, if this will lead t...