lambda

Sort list by property/anonymous function?

I've got a list defined like this... var sets = new List<HashSet<int>>(numSets); Why isn't there an overload so I can sort it like this? sets.Sort(s => s.Count); I want the largest set first. What's the easiest way to do that? ...

Having problems with this Linq/Lambda statement :(

Hi folks, Update - I fixed the query below. I had the wrong query/error statement :( I have the following statement: var posts = BlogPostRepository.Find() .Where(x => x.Tags.Where(y => y.Name == tag)) .ToList(); It's giving me a compile time error with the 2nd (inner) Where clause, saying :- Error 1 Cannot convert...

Lambda expressions, captured variables and threading

I know that .NET lambda expressions can capture outer variables. However, I have seen it a lot of times that variables are passed explicitly to the lambda expression as a parameter, and the .NET library also seems to support that (e.g. ThreadPool.QueueUserWorkItem). My question is that what are the limitations of these captures? How abo...

Is there a 3rd party library that knows to convert a linq expression to a human readable string representation?

I have a linq expression and I wish to display it in the log in a human readable form. Any one knows any library that can do it? I saw this entry http://stackoverflow.com/questions/3294438/creating-a-string-from-a-lambda-expression, but it is not that useful, in my opinion. Thanks. EDIT Now that I think about it, my case is probably n...

Casting IEnumberable<T> back to original type with lambda expression

If I have the following how to do I cast the result of the lambda expression back to the Customer type from IEnumerable without having to iterate over it. public class Customer : CustomerModel { public List<Customer> CustomerList {get;set;} public Customer GetCustomerFromListById(long id) { retur...

Why cant I use lambda when serializing DataContract?

Made som mock code below to illustrate my example. The problem is the lambda expression. If I leave it as in the code example it will not serialize when I try to call the service. However if I type .ToList() after the lambda it serializes as it should. Why is that? I can't see why the code below should not work... Anyone care to enlight...

How does this Python Lambda recursion expression work?

rec_fn = lambda: 10==11 or rec_fn() rec_fn() I am new to Python and trying to understand how lambda expressions work. Can somebody explain how this recursion is working? I am able to understand that 10==11 will be 'false' and that's how rec_fn will be called again and again recursively. But what I am not able to get is this seemingly ...

Comparision : LINQ vs LAMBDA Expression

i need a discussion regarding the Performance of LINQ and Lambda Expression. Which one is better?? ...

Is there a difference between these two lambda expressions?

What is the difference if any between these two Lambda Expressions ? And the second one seems more compact should I always go for that ? DataContext.Employee.Where(c=>c.id==check_id && c.username==user_name).Select(c=>c.Name).FirstOrDefault(); and DataContext.Employee.FirstOrDefault(c=>c.id==check_id && c.username==user_name).Name; ...

LINQ: Expression.Call(typeof(Queryable), "Select"...

I'm attempting to create a small "automapper-esq" utility that will take a LinqToSql entity and map it to a "projection class". So far I have something like this: class Entity { public int ID { get; set; } public string WantedProperty { get; set; } public string UnWantedPropertyData { get; set; } ...More Unwanted Proper...

Map Func<Type1, bool> to Func<Type2, bool>

I'm using Entity Framework behind a set of repositories. The repositories use DTOs instead of the entity objects generated by the Entity Framework. I'm using AutoMapper to convert them back and forth. I'm having trouble figuring out how to do the following: Method definition: public IEnumerable<DTO.User> Get(Func<DTO.User, bool> where...

Error Checking using Func Delegate

So i recently learned this new trick of using Func Delegate and Lambda expression to avoid multiple validation if statements in my code. So the code looks like something like public static void SetParameters(Dictionary<string,object> param) { Func<Dictionary<string, object>, bool>[] eval = { ...

How to add data to DataGridView

I'm having a Structure like X={ID="1", Name="XX", ID="2", Name="YY" }; How to dump this data to a DataGridView of two columns The gridView is like ID | Name Can we use LINQ to do this. I'm new to DataGridView Pleaese help me to do this.. Thanks in advance ...

Like operator in Expression Tree

I have a Linq extension method to dynamically filter Linq queries using string values. For example: query.WhereHelper("columName", ">", 1). I could use many different filter operators like GreaterThan or NotEqual etc. but not "Like". There is no Expression.Like or Expression.StartsWith etc. How can I implement Like operator to my Express...

AddRange/concat functionality inside a lambda Select expression

class Foo { int PrimaryItem; bool HasOtherItems; IEnumerable<int> OtherItems; } List<Foo> fooList; How do I get a list of all item ids referenced inside fooList? var items = fooList .Select( /* f => f.PrimaryItem; if (f.HasOtherItems) AddRange(...

Use interface to convert collection of objects with extensions and lambdas

I have some objects like user, address and so on, and Im converting them to business objects using extension methods: public static UserModel ToPresentationForm(this User pUser) { return new UserModel { ... map data ... }; } Also I need to convert strongly typed collections...

query only returning one result

Hey, I am pretty certain I am doing something incorrectly with my lambda expression and that is making it so that I only return a single result. If there is nothing wrong with my query (it has the ability to return multiple results), then I must have an error elsewhere and I should be able to find it. If my results should return more tha...

Smalltalk blocks in Objective-c?

Does Objective-C support blocks "a la Smalltalk"? In Smalltalk, blocks are similar to "closures" or "lambda-expressions" or "nameless functions" found in other languages. ...

Conditional Lambda Expression?

I have a lambda expression that currently looks like this: item => Reports.Add(item) I want to modify it such that it is conditional, and basically checks that Reports.Contains(item) returns false, then performs the Reports.Add(item) action. Is this possible to do using lambda all on one line? Chris ...

Closure scope and garbage collection

I've just written some code to perform a timeout action if an asynchronous task takes too long to process, but what is not clear to me is if and when the timeout instance will ever be disposed of (I think it will in the case where the asynchronous task completes in a timely fashion, but otherwise I've got no idea), or if I'm going to be ...