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 = ...
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)
{
...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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?
...
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 lambda expressions objects? If so, what sort of object are they?
...
In performance point of view what should you use "Nested foreach's" or "lambda/linq queries"?
...
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...
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
...
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 ...
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...
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...