The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation

The issue is that you are trying to do a string.Contains within an Any expression which EF will choke on trying to compose down to SQL. Cleptus is on the nose, to build a predicate for the Where clause OR-ing the term comparisons. Otherwise your code should work without the contains check, but rather an … Read more

How does deferred LINQ query execution actually work?

Your query can be written like this in method syntax: var query = numbers.Where(value => value >= threshold); Or: Func<int, bool> predicate = delegate(value) { return value >= threshold; } IEnumerable<int> query = numbers.Where(predicate); These pieces of code (including your own query in query syntax) are all equivalent. When you unroll the query like that, … Read more

How to use Expression to build an Anonymous Type?

You’re close, but you have to be aware that anonymous types don’t have default constructors. The following code prints { Name = def, Num = 456 }: Type anonType = new { Name = “abc”, Num = 123 }.GetType(); var exp = Expression.New( anonType.GetConstructor(new[] { typeof(string), typeof(int) }), Expression.Constant(“def”), Expression.Constant(456)); var lambda = LambdaExpression.Lambda(exp); object … Read more

How set value a property selector Expression

This works: The following helper method converts a getter expression into a setter delegate. If you want to return an Expression<Action<T,TProperty>> instead of an Action<T,TProperty>, just don’t call the Compile() method at the end. Note: The code is from Ian Mercer’s blog: http://blog.abodit.com/2011/09/convert-a-property-getter-to-a-setter/ /// <summary> /// Convert a lambda expression for a getter into a … Read more

Cannot implicitly convert type ‘bool’ to ‘system.threading.tasks.task bool’

You need to be specific whether you want this operation happen asynchronously or not. As an example for Async Operation : public async Task<bool> login(string usn, string pwd) { DataClasses1DataContext auth = new DataClasses1DataContext(); var message = await (from p in auth.Users where p.usrName == usn && p.usrPass == pwd select p); if (message.Count() > … Read more