lambda

A more efficient way to manage IEnumerable.GroupBy()

This is a segue to my previous question: http://stackoverflow.com/questions/3141285/looking-for-a-better-way-to-sort-my-listt Basically, I have a similar situation where I need to do a .GroupBy() on about 40 different fields. The original code would have used a giant switch statement, but I'd like to know if there's a better way to th...

Can I use lambdas in a VB ASP.NET (2.0) project in VS 2008?

I built a small chunk of code based around lambdas in a VB Windows form project earlier which works perfectly, but it gives me "expression expected" warnings (which block compiling... should probably be considered errors, no?) when I copy the code to an ASP.NET project. The only difference I can see is if I make a Windows form project v...

How to invoke Func to set a local List<>

I guess I am missing something here but can someone explain how I can get this to work I have a method that takes a Func, I want to execute that func in the method a store the result in a local var. internal List<MembershipUser> Users; internal void FindType<T>(Func<List<MembershipUser>, T> finder)where T : List<MembershipUser> { ...

lambda expression - equivalent of sql outer join

Hello. I need to write the method in C# that will return some specific dictionary from DB. Lets say that data stored in 2 tables looks like: table Groups (id, name): 1, 'Management' 2, 'IT Department' 3, 'Production' The Users (id, name, groupId, projectId): 1, 'John', 1, 1 2, 'Ben', 2, 1 Now I need to check that in...

How would I add type specific implementations to linq binary expressions?

I have a fairly simple expression parser that uses the Linq.Expression namespace. Input is something like: (1+1), it finds the left and right constants, and converts the operator char to an ExpressionTypes enum value, creates the appropriate expression, compiles and executes. I'd really like to be able to do string manipulations, (abc+...

How do I efficiently compare LINQ expression instances for equality?

Possible Duplicate: Most efficient way to test equality of lambda expressions I was wondering how LINQ expression instances compare for equality. It seems that the expression classes do not implement IEquatable or IComparable or anything alike. Let me give you some background information: I've implemented a caching mechanism ...

How to pass parameters to LINQ to XML query by using lambda expression ?

I am new to LINQ to XML in .net(C#, asp.net). I want to know more about Lambda expressions. I am using Lambada expression with the following query. count = FieldRoot.Element("USER").Element("MM") .Descendants("MMMS") .Where(a => a.Attribute("ID").Value == MID) .SelectMany(b => b.Descendants("ABC")).Co...

Which method should be used instead of descendants() in LINQ to XML ?

I am developing asp.net mobile application. I am using XML as a database. I am using the following query for required output. In the XML file I have the collection of MIMIC nodes & inside MIMICS node I have collection of SECTION nodes. One or more more SECTION node contains one or more SECTION nodes ( nested SECTION node) In the SECTION ...

What is actually happening with IQueryable.Where()?

This is is returning a boolean based on whether or not there are some matching IDs. from t in getAll select new Result { ... bool DetailsAvailable = (db.SaveTrackings.Where(s => s.BundleID == t.bundleID && s.UserID == t.userID) .Count() > 0) ? true : f...

boost lambda question

#include <iostream> #include <set> #include <algorithm> #include <boost/lambda/lambda.hpp> #include <boost/bind.hpp> using namespace std; using namespace boost::lambda; class Foo { public: Foo(int i, const string &s) : m_i(i) , m_s(s) {} int get_i() const { return m_i; } const string &get_s() const { return m_s; } fri...

Passing an expression tree as a parameter to another expression tree

I have two expression trees defined like this: private Expression<Func<TEntity, TPropertyResult>> PropertyAccessor { get; set; } and private Expression<Func<TPropertyResult, bool>> TestExpression { get; set; } I need to create a new expression tree that will result in the equivalent of: var expression = p => this.TestExpression(th...

Whats is lambda method in Linq to XML?

Whats is lambda method in Linq to XML? how to use it? what is its main purpose ? in .net ...

Getting error while executing Ienumerable<Datarow>.Sum() method.

Why isn't the below code working? I am getting an error. It says 'Cannot implicitly convert type 'V' to int ' . private ObservableCollection<DataItem> method<T, V>(DataTable dt, SeriesItem seriesItem, FilterValues filter, ObservableCollection<DataItem> chart) { var result = from t in dt.AsEnumerable() ...

How to cache 'IQueryable<>.First' result?

I have 2 similar queries: ICmOption optionRes = CmOptionRepository<ICmOption> .GetAll() .Where(option => option.Name == strCommandName && option.Data == strCommandOption) .FirstOrDefault() ; IErrorType errorType = ErrorTypeRepository<IErrorType> .GetAll() ...

SelectList Extension - couple expressions

So I got this crazy idea I could make make something cool work. I got tired of new selectlist(item, "blah", "blahblah") so I started writing an extension method (trying to get it more strongly typed) something like this ... var selectList = projects.ToSelectList(p =>p.ProjectID, p =>p.ProjectName); the extension method goes a little ...

How can I get parameters values from a lamba expression for my nifty cache extension?

First of all it might be worth looking at this question: http://stackoverflow.com/questions/445050/how-can-i-cache-objects-in-asp-net-mvc There some pseudo code that almost does what i want: public class CacheExtensions { public static T GetOrStore<T>(this Cache cache, string key, Func<T> generator) { var result = cache[key]; ...

Using a lambda expression versus a private method

I read an answer to a question on Stack Overflow that contained the following suggested code: Action<Exception> logAndEat = ex => { // Log Error and eat it }; try { // Call to a WebService } catch (SoapException ex) { logAndEat(ex); } catch (HttpException ex) { logAndEat(ex); } catch (WebException ex) { logAndEat...

Converting 2 argument Lambda expression to 1 argument Lambda expression (specifing one argument)

I have expression Expression<Func<Car, Driver, bool>> CanBeDrivenBy = (car, driver) => car.Category == 'B' && driver.Age > 18; and I want to get cars which can be driven by some driver IQueryable<Cars> cars = ...; Driver driver = ...; cars.Where(CanBeDrivenBy); // Fail, expecting Expression<Func<Car, bool>> So I need to conv...

Specification pattern - creating compound specifications using lambdas (C#)

If I have a specification defined as an Expression as below: public Expression<Func<Foo, bool>> IsSuperhuman = x => x.CanFly && x.HasXRayVision; And I want to define another specification 'IsSuperheroine' with the logic 'is superhuman and is female', how can I reuse the existing specification within the new one? ...

C# lambda expression in LINQ query

Hi all I know that lambda expressions within loops can cause problems if they use local variables. ( see http://www.jaylee.org/post/2008/11/18/Lambda-Expression-and-ForEach-loops.aspx ) Now I have a situation, where I am using a lambda expression within a LINQ query: var products = from product in allProducts select new...