lambda

How can I reuse expressions within LINQ statements?

I like to reuse expressions for DRY reasons, but how do I reuse the expressions within a LINQ statement? e.g. I have public static class MyExpressions { public static Expression<Func<Product,bool>> IsAGoodProduct() { return (p) => p.Quality>3; } } And would like to use that in LINQ statements, so var goodProds = ...

LINQ with generic Predicate constraint

I just upgraded my project to .NET 3.5 and thought I had a perfect use for LINQ. My application has a window manager that tracks open windows at runtime and I'm trying to add a FindOpenWindows generic method. What I've done so far: List<Form> openWindows; public List<T> FindOpenWindows<T>(Predicate<T> constraint) { ...

How do you filter a collection of objects in a base class based on the type of the sub class created?

I wrote this example to help explain. As you can see I have an object hierarchy. I'd like to modify the GetFeatures() function to only return the features added by constructor of object type I instantiated. For example, BasicModel.GetFeatures(new LuxuryModel()) should only return the features "Leather Seats" and "Sunroof". I don't mind u...

combining lambda expressions to property paths

I'd need to be able to combine 2 lambda expressions into 1: This would serve me to create an extension to the type-safe includes (for EF). Now you can do: context.House .Include(x => x.Doors.Doorknobs) I'd like to be able to split up the above statement into different methods. something like IncludeDoorKnobs(query, expressionFr...

Convert Method Group to Expression

I'm trying to figure out of if there is a simple syntax for converting a Method Group to an expression. It seems easy enough with lambdas, but it doesn't translate to methods: Given public delegate int FuncIntInt(int x); all of the below are valid: Func<int, int> func1 = x => x; FuncIntInt del1 = x => x; Expression<Func<int, int>> f...

Need help on developing LINQ Lambda expresssion to filter on a Schedule

I am having a hard time finding the lambda expression to call on a list to correctly filter an object structure. I was hoping someone here could help out. I am using .NET 3.5, and LINQ and the object domain is set up from Linq to SQL DBML. I specifically want to use lambda expressions. The object structure is one where persons have s...

Querying a Dictionary with a Lambda Expression

I've created some sample code below, and am trying to use a lambda expression to query against the SoftwareComponents Dictionary. The problem is that the query returns a var of type IGrouping, when what I need to do is further refine the query so that it returns a type of IGrouping where the first string is the SoftwareComponent.Compone...

Another LINQ to XML (C# to VB.NET conversion)

What is the VB.NET syntax for these? Any help converting would be greatly appreciated. var defaultStyleName = (string)doc .MainDocumentPart .StyleDefinitionsPart .GetXDocument() .Root .Elements(w + "style") .Where(style => (string)style.Attri...

What's the equivalence of an SQL WHERE in Lambda expressions?

Hi, Here's what I have: decimal sum = _myDB.Products.Sum(p => p.Price).GetValueOrDefault(); I also have two dates: DateTime start, DateTime end I want to retrieve the sum of all of the product prices between start and end, but I can't figure out how to incorporate the variables into the lambda equation. How do you incorporate variab...

What would LINQ to Entities return when selecting an integer id for a given row name if the record does not exist?

Here's the code: string name = "myName"; int id = (int)_myDB.ThingTable.Where(thing => thing.ThingName == name) .Select(thing => thing.ThingId); I have an error saying System.Linq.IQueryable cannot be converted to int (I'm assuming it's so that I don't end up with a case where no rows are found- no id is returned...

Stumped on Entity Framework & Lambda Expressions

I know currently the compiler is not liking this statement. Getting Error Cannot convert lambda expression to delegate type 'System.Func<MyData.Models.SomeModels,bool>' because some of the return types in the block are not implicitly convertible to the delegate return type My Statement I'm passing to my Repository Class var qry = rep...

What is the recommended way of writing anonymous functions in C#?

var seq = Enumerable.Range(1, 10).Reverse(); var sort1 = seq.OrderBy(i => i); var sort2 = seq.OrderBy(delegate(int i) { return i; }); i think sort2 is more explicit but sort 1 is shorter. besides that, i don't really know the difference. what is the recommended way of doing this? ...

Usage Of Lambda Expression in Practical Programming

I am an experienced developer in C# and working on LOB application from last 7 Years, I have some issues in understanding the usage of Lambda expression in programming. As far as I understand, it is useful in case of Working with LINQ (Grouping, Select,Where etc..) We can pass Lambda expression to any function as argument, so it can ...

In C#, are Lamba Expressions objects?

In C#, are lambda expressions objects? If so, what sort of object are they? ...

"Nested foreach" vs "lambda/linq query" performance(LINQ-to-Objects)

In performance point of view what should you use "Nested foreach's" or "lambda/linq queries"? ...

When to use lambda expressions instead of a Where clause in LINQ

I've been really digging into LINQ, and I'm trying to hash out this lambda expression business. I'm just not seeing the benefit of some of the nuances of the syntax. Primarily, it seems to me that a lambda expression is mostly just a different way of using a Where clause. Why wouldn't I just use a Where clause then? Is the lambda expres...

using And in predicate for lambda expression

Hi how to use "And" in predicate in lambda expression. I am trying to achieve something like this new Class1("Test",Status => Status == 18 && 19 && 20) Please reply Thanks Sharath ...

C#: How do you pronounce =>

Possible Duplicate: How do I pronounce "=>” as used in lambda expressions in .Net? What do i read => as? As in: listOfFoo.Where(x => x.Size > 10); If i used the standard mathematical notation it reads as implies. So the entire line becomes: x implies x's Size property is greater than ten That makes no sense; so i'm unable ...

LINQ to Entities: Using DateTime.AddMonth in Lambda Expression

Hello, I've been reading some posts but I don't find a solution to a problem that I have with LINQ To Entities, Lambda Expressions and DateTime.AddMonth. The problem is that I'm trying to use DateTime.AddMonth inside a Lambda Expression and I'm getting this error: "LINQ to Entities does not recognize the method 'System.DateTime AddMo...

Using Lambda with Dictionaries

Hello, I am trying to use LINQ to retrieve some data from a dictionary. var testDict = new Dictionary<int, string>(); testDict.Add(1, "Apple"); testDict.Add(2, "Cherry"); var q1 = from obj in testDict.Values.Where(p => p == "Apple"); var q2 = from obj in testDict.Where(p => p.Value == "Apple"); The above lines, q...