linq-expressions

What is the best way to merge two objects during runtime using C#?

I have two objects and I want to merge them: public class Foo { public string Name { get; set; } } public class Bar { public Guid Id { get; set; } public string Property1 { get; set; } public string Property2 { get; set; } public string Property3 { get; set; } public string Property4 { get; set; } } To create:...

How to I find specific generic overload using reflection?

I am attempting to create an Expression that will invoke a specific generic overloaded method (Enumerable.Average in my first test case). The specific type bindings are not known until runtime however so I need to use Reflection to find and create the correct generic method (the Expression is being created from parsed text). So if I kno...

Does a function call in the Where(...) clause of a Linq to Nhibernate query negatively affect performance?

I use linq to nhibernate and the IQueryable.Where function in an application I'm building. And what mystifies me is how do the Expressions I create and pass to the Where function of a INhibernateQueryable affect performance. I'm not really sure what are the gotchas I should avoid in writing these query Expressions, in terms of perfor...

LINQ - Minimize Records Returned - Correct Way to Write These Expressions

Employee is a sample entity type. var r1 = (from c in _ctx select c).Skip(5).Take(5); // my intent is to pull the first record from the query var r2 = (from c in _ctx select c).FirstOrDefault<Employee>(); // my intent is to pull the last record from the query. // any good way to ask for the result back in the reverse // orde...

WhereNot linq expression

Hello I am trying to create an extension "WhereNot" So I can use: Dim x = "Hello world " Dim y = x.Split.WhereNot(AddressOf String.IsNullOrEmpty) Note that my aim here is to learn linq expressions; not solve my issue. I craated this function: <Extension()> _ Public Function WhereNot(Of TElement)(ByVal source As IQueryable(Of TEl...

How do I compose Linq Expressions? ie Func<Exp<Func<X, Y>>, Exp<Func<Y, Z>>, Exp<Func<X, Z>>>

I'm creating a Validator<T> class. I'm attempting to implement the Linq SelectMany extension methods for my validator to be able to compose expressions using a Linq query and validate the final result even when the underlying values change. The following test code demonstrates my intent. var a = 2; var b = 3; var va = Validator.Create...

How do I Emit a System.Linq.Expression?

I've got some code that generates various Func<> delegates using System.Linq.Expressions and Expression.Lambda<Func<>>.Compile() etc. I would like to be able to serialize the generated functions into an assembly for later use. In the past I've done some stuff with System.Reflection.Emit but now that Linq Expressions I'd rather not go tha...

LINQ Expression<Func<T, bool>> equavalent of .Contains()

Has anybody got an idea of how to create a .Contains(string) function using Linq Expressions, or even create a predicate to accomplish this public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expr...

Should methods containing LINQ expressions be tested / mocked?

Assuming I have a class with a method that takes a System.Linq.Expressions.Expression as a parameter, how much value is there in unit testing it? public void IList<T> Find(Expression expression) { return someCollection.Where(expression).ToList(); } Unit testing or mocking these sorts of methods has been a mind-frying experience fo...

How do I access a Dictionary Item using Linq Expressions

I want to build a Lambda Expression using Linq Expressions that is able to access an item in a 'property bag' style Dictionary using a String index. I am using .Net 4. static void TestDictionaryAccess() { ParameterExpression valueBag = Expression.Parameter(typeof(Dictionary<string, object>), "valueBag"); Paramet...

What is the purpose of LINQ's Expression.Quote method?

The MSDN documentation states: Expression.Quote Method Creates a UnaryExpression that represents an expression that has a constant value of type Expression. I've been able to build predicate expressions for use in LINQ queries by manually constructing them using the Expression class, but have never come across the need f...

Using Linq expressions as a specification pattern with parent/child query

I'm trying to use the specification pattern implemented as a Linq expression so that Linq providers can parse it to produce efficient database queries. This gives the basic idea. I am having a hard time trying trying to get it working with a parent/child query class Parent { public int Foo; public IList<Child> Children = n...

How to Transform a LINQ Expression when you do not have one of the parameters when you define it.

I'm trying to build more generic query functionality into my application. What I'd like to do is define objects which given an predicate expression can apply that to an iqueryable with a value that will be passed in later. I believe the code below should demonstrate what I'm trying to do well enough to understand the problem. Please let...

Invoke an Expression in a Select statement - LINQ to Entity Framework

I'm trying to use an already existing Expression building class that I made when trying to do a select clause, but I'm not sure how to attach the expression to the expression tree for the Select, I tried doing the following: var catalogs = matchingCatalogs.Select(c => new { c.CatalogID, ...

Linq to Nhibernate - Compare 2 lists

I have 2 lists and I need to know if there are any matches. I've tried using request.Interests.Intersect(x.Post.Tags.Split(' ')).Count() > 0 but I get the error System.NotImplementedException : The method Intersect is not implemented. So, I tried a recursive function that returns a bool. And it's as if the function call is jus...

Why are some object properties UnaryExpression and others MemberExpression?

Acting on the answer to my Select a model property using a lambda and not a string property name question, wanting to add properties to a collection as follows: var props = new ExportPropertyInfoCollection<JobCard>(); props.Include(model => model.BusinessInstallNumber).Title("Install No").Width(64).KeepZeroPadding(true); props.Include(...

Convert MethodBody to Expression Tree

Is there a way to convert a MethodBody (or other Reflection technique) into a System.Linq.Expressions.Expression tree? ...

Expression<Func<T,bool>> - How to Handle Ambiguous Method Signatures?

Hi Guys, I have an interface contract that looks like this: ICollection<FooBar> FindByPredicate(Expression<Func<FooBar,bool>> predicate); ICollection<Foo> FindByPredicate(Expression<Func<Foo,bool>> predicate); ICollection<Bar> FindByPredicate(Expression<Func<Bar,bool>> predicate); Foo and Bar are concrete classes which inherit from t...

C# coercion operator?

I got this test: [Fact] public void EverythingIsMappedJustFine(){ new AutoMapperTask().Execute(); Mapper.AssertConfigurationIsValid(); } It throws a bit strange exception: Test 'Unit.Web.Bootstrap.AutoMapperFacts.EverythingIsMappedJustFine' failed: System.InvalidOperationException : No coercion operator is defined betwee...

Consolidate or reuse LINQ expression

I have a LINQ expression that gets used as a filer in a LINQ to SQL statement where clause. My problem is that the LINQ to SQL expression has become unwieldy and also the logic it contains has ended up in multiple locations violating DRY (this is the 3rd time I'm working on a bug raised by QA for it getting out of sync). Is there any wa...