Hi, I am working on an ASP.Net MVC application which uses the repository pattern with linq to sql as my data source. In my repository I expose the following method:
public IEnumerable<T> Find(Expression<Func<T, bool>> where)
{
return _context.GetTable<T>().Where(where);
}
I am able to call this by saying:
repository<User>.Find(u...
Ok, I just don't get it.
I've read about as much as I can on the subject without knowing what it's all about:
Why use Expression Trees?
What's a real-world example of when and how I'd use them?
What overall benefit(s) are there in using them?
...
All,
Below is the lambda expression which I am finding difficult to reduce i.e. I am not able to understand how to go about this problem.
(λm λn λa λb . m (n a b) b) (λ f x. x) (λ f x. f x)
This is what I tried, but I am stuck:
Considering the above expression as : (λm.E) M equates to
E= (λn λa λb. m (n a b) b)
M = (λf x. x)(λ f x....
Can anyone advise why this line of code would not compile? It generates CS1660 instead:
s.run_button.Invoke((b) => { b.Enabled = false; },
new object[] { s.run_button });
Visual studio says: Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type
...
I'm building a LINQ expression tree but it won't compile because allegedly the local variable $var1 is out of scope:
variable '' of type 'System.Object' referenced from scope '', but it is not defined
This is the expression tree:
.Block() {
$var1;
.If ($n.Property1 == null) {
.Block() {
$var1 = null;
...
I have 3 methods that are almost exactly identical:
protected DetachedCriteria GetAvailableFundIdsPerDataUniverse()
{
return GetAvailableIdsPerDataUniverse()
.SetProjection(LambdaProjection.Property<Fund>(f => f.Id));
}
protected DetachedCriteria GetAvailableCompanyIdsPerDataUniverse()
{
return GetAvailableIdsPerDataUni...
I have a fluent validation API that throws an exception when a supplied boolean result is false. This works fine.
I'd like to improve upon this by, instead of throwing an exception, providing some functionality to execute if the result is false, and then forcing the calling method to return. Example, the supplied functionality might new...
private void ExecuteCommand(Expression<Func<bool>> command)
{
bool success = command.Compile().Invoke();
}
private void Test()
{
ExecuteCommand(() => _gc.ChargeCancellation(""));
}
With this code, I got a NullReferenceException.
...
I have the following statement that's throwing an error I don't understand:
return (int) _session.CreateCriteria<T>()
.Add(LambdaSubquery.Property<Fund>(x => x.Id)
.In(GetAvailableIdsPerDataUniverse(x => x.GetDataUniverseId())))
.AddNameSearchCriteria<T>(searchExpression)
.SetProjection(LambdaProjection.Count<T>(e =>...
I'm working on a method that needs to repeat a small operation at different spots, but the code to be repeated should be private to the method. The obvious solution is a nested function. Whatever I try however, the C# compiler barfs at me.
Something roughly equal to this Perl snippet:
my $method = sub {
$helper_func = sub { code to...
I have a tree expression that looks like this:
.Block(
System.Object $instance,
MyType2 $result) {
$result = (MyType2)((MyType1)$instance).Property1;
.Goto return { };
.Label
.LabelTarget useDefault:;
$result = .Default(MyType2);
.Label
.LabelTarget return:;
$result
}
These are the custom types ...
F# powerpack comes with a set of conversion methods to translate from Func<...> to F# functions, either standard or tupled ones. But is it possible to achieve the opposite: in case you want to call from F# code a C# method that takes Func<...> and want to use native F# lambda expression (e.g. fun x -> some_function_of(x))?
If I send a F...
I would have expected delegates generated from expression trees to achieve roughly the same performance as hard-coded, static, equivalent anonymous methods. However, it seems that dynamically generated delegates are noticeably slower...
Here's a simple test program to illustrate the case. It just accesses 3 properties of an object 10000...
Forgive me if this has been asked already. I've only just started using LINQ. I have the following Expression:
public static Expression<Func<TblCustomer, CustomerSummary>> SelectToSummary()
{
return m => (new CustomerSummary()
{
ID = m.ID,
CustomerName = m.CustomerName,
LastSalesContact = // This is a Per...
i understand that predicates are delegate to function which return bool and take generic param , i understand that when i say
mycustomer => mycustomer.fullname == 1
it actually means:
delegate (Customer mycustomer)
{
return mycustomer.fullName == "John";
}
the paramter im passing in when i ...
I often do sorts in Python using lambda expressions, and although it works fine, I find it not very readable, and was hoping there might be a better way. Here is a typical use case for me.
I have a list of numbers, e.g., x = [12, 101, 4, 56, ...]
I have a separate list of indices: y = range(len(x))
I want to sort y based on the value...
I have a custom sort routine that works great, but I was curious about how I would convert it to use less code with LINQ or lambda. SortByGrade sorts a string value representing a grade ( K - 12 ) so that it returns: K, 1, 2, 3, 4, etc. instead of 1, 10, 2, etc
/// <summary>
/// Sorts a grade ( K - 12 ) numerically
/// </su...
I have a collection of boxes with the properties weight, volume and owner.
I want to use LINQ to get a summarized list (by owner) of the box information
e.g.
**Owner, Boxes, Total Weight, Total Volume**
Jim, 5, 1430.00, 3.65
George, 2, 37.50, 1.22
Can someone show me how to do this with Lambda expression...
In scheme, why is this:
(define foo
(lambda (x)
42))
considered better style than this:
(define (foo x)
42)
And is there any reason to favour one over the other?
...
how to order descending an IEnumerable<T> with linq or lambda ?
...