Create case sensitive LINQ Expression with Contains method (Case insensitive collation database) - linq

I have a database created with a SQL_Latin1_General_CP1_CI_AS collation (which is case insensitive).
I'm creating LINQ Expressions and trying to make "Contains" method which should compare strings in a case sensitive way.
I know that I can force collation if I use something like:
CHARINDEX(N'foo' COLLATE SQL_Latin1_General_CP1_CS_AS, 'something Foo') > 0
but since I'm building expressions using LINQ to create a query, my code looks like:
using System.Linq.Expressions;
private Expression Contains(Type type, string value, Expression propertyExpression)
{
var searchValue = Expression.Constant(value, typeof(string));
var method = propertyExpression.Type.GetMethod("Contains", new[] { type });
var result = Expression.Call(propertyExpression, method, searchValue);
...
}
So, if I'm trying to use this Contains method for word 'foo', rows with 'Foo' will also be returned (which I don't want).
Is there a way that I can expand this Expression so that I can specify Case Sensitive comparison?

Related

How to add a second where clause to a linq expression

Trying to add a second where clause to a linq expression but it won't register.
var query = _dbSetBookedResource.AsQueryable<Resource>();
var resources = (from Resource in query where Resource.DateFrom == date select Resource)
if(true)
{
resources.Where(b => b.MemberId == currentUserId);
}
For some reason the second where clause won't register.
For some reason the second where clause won't register.
That's because you're not using the return value anywhere. That's just setting up a query, but then ignoring it. No LINQ methods change the value they're called on - instead they create a new query which has the appropriate filtering, projection etc.
You need:
resources = resources.Where(b => b.MemberId == currentUserId);
Also note that your initial query could be written more simply as:
var resources = query.Where(r => r.DateFrom == date);
Query expressions are overkill when all you want is a simple filter or projection.

Convert string value to entity in linq where query

I am using jqgrid in MVC 4. I have written a method for getting a list in linq.
In my function I am getting all values of jqgrid with search criteria i.e. Operator AND/OR
, operations Equals to, not equals to etc. Here I am also getting the column name like Name, City, State etc.
My problem is I can't pass the column name directly in linq query i.e. I have to use the column name as x => x.Name
switch (rule.field)
{
case "Name":
query = query.Where(x => x.Name.StartsWith(rule.data, StringComparison.OrdinalIgnoreCase));
break;
case "Course":
query = query.Where(x => x.Course.StartsWith(rule.data, StringComparison.OrdinalIgnoreCase));
break;
}
In rule.field I am getting column Name i.e. Name, city, state etc. I want to pass the column name which I am getting in rule.filed in LINQ query instead of x =>x.Name.
Is there any way to do it so I can avoid writing switch cases?
You can use System.Linq.Dynamic, which can be installed as a Nuget package along with string.Format. The syntax would then look something like..
var newquery = query.AsQueryable()
.Where(
string.Format("{0}.ToUpper().StartsWith(#0)", rule.field)
,rule.data.ToUpper());
You could always use reflection:
query = query.ToList().Where(p => {
var field = p.GetType().GetProperty(rule.field);
var value = (String) field.GetValue(p);
return value.StartsWith(rule.data, StringComparison.OrdinalIgnoreCase);
});
Warning: Reflection is slow. Only use this for short lists. Since you're dealing with UI rendering, I'm assuming this won't be a problem.
edit: My example is assuming all properties are indeed properties (and not fields), and that all properties are Strings. You may need to alter the code for your specific case.

Using String.compare inside a lambda expression

I have the following model method:
public IQueryable<CustomerVLAN> CustomerVLANS(int customerid)
{
string customername = entities.AccountDefinitions.SingleOrDefault(a=>a.ORG_ID == customerid).ORG_NAME;// .tolist since i will be querying different context
return tms.CustomerVLANS.Where(String.Compare(a=>a.CustomerName.ToString(), customername.ToString(), StringComparison.OrdinalIgnoreCase));
}
but I am unable to use the String.Compare inside my where clause ,,,
Provided CustomerName and customername are strings, you can try
return tms.CustomerVLANS.Where(a=>a.CustomerName.ToUpper() == customername.ToUpper());
The Where extension filters an IEnumerable using a supplied predicate function: Func<TSource, bool>, in your case Func<CustomerVLAN, bool>.
Your line "String.Compare(a=>a.CustomerName.ToString(), customername.ToString(), StringComparison.OrdinalIgnoreCase)" returns an integer, rather than a Func<CustomerVLAN, bool> delegate (I'm also not sure if there's an overload of string Compare which expects the values you've provided).
If you wanted to use that particular function, you'd have to do something like this (using a Lambda expression):
CustomerVLANS.Where(x => (String.Compare(x.CustomerName, customername, StringComparison.OrdinalIgnoreCase) == 0));
Unless there's a particular reason for it, you may well be better of using String.Equals, which returns a boolean:
CustomerVLANS.Where(x => (String.Equals(x.CustomerName, customername)));
You may need your ToString(), depending on what type CustomerName is.

Linq statement fails with dynamic where clause

I want to retrieve commissions with a certain order number.
This works:
var expression = from commission in db.Auftraege
where commission.Auftragsnummer == orderNbr
select new Commission() { EF_Commission = (commission as Auftrag) };
return expression.ToList();
However, if i transform this to use a dynamic where clause (because i want to apply some more filters), the where-clause does not seem to be applied. Instead, all commissions are returned instead of only those with a specific number:
//base query
var expression = from commission in db.Auftraege select new Commission() { EF_Commission = (commission as Auftrag) };
//now add a where clause if the input parameter was specified
if (orderNbr >= 0)
expression.Where(commission => commission.EF_Commission.Auftragsnummer == orderNbr);
return expression.ToList();
I have looked at a dozen examples but they all seem to do it this way. Does anybody have an idea why the second query ignores the where clause?
You need to assign the interim expression to something (perhaps to itself). expression.Where() does not alter the existing query - it returns a new one.
So:
expression = expression.Where(...);

Linq expression subsonic 3.0.0.3

I want to 'build' a combined query for Subsonic 3.0.0.3, what is the best way for this?
I tried;
Expression<Func<Person, bool>> exp = p => true;
Expression<Func<Person, bool>> fContinent = p => p.ContinentID == 1;
Expression<Func<Person, bool>> fType = p => p.TypeID == 1;
exp = Expression.Lambda<Func<Person, bool>>(Expression.AndAlso(exp, fContinent), exp.Parameters);
exp = Expression.Lambda<Func<Person, bool>>(Expression.AndAlso(exp, fType), exp.Parameters);
var personList = Person.Find(exp);
But that will give the exception "The binary operator AndAlso is not defined ..."
I also tried using predicates but that will throw exceptions as well (Expression.Invoke is not supported).
In subsonic 2 I would have used the SqlQuery object, but I would like to know the proper way to do this in version 3 using linq / expressions.
Have you tried And instead of AndAlso?
The right way to do this is to combine the lambda expression bodies, like this:
exp = Expression.Lambda<Func<Person, bool>>(
Expression.And(exp.Body, fContinent.Body), exp.Parameters);
Even if And is supported by your query provider, you'll also need to replace the parameter references in fContinent's Body with references to the parameter defined in exp--as is, your two expression bodies (combined with And) reference two distinct parameters, each named p.
See my answer to this question for the cleanest method to replace expression parameters.
I asked this question, but I am using the combined query in subsonic just like you.
In short, you want to use a PredicateBuilder to build up the query. When you want to execute it in your subsonic object (assuming ActiveRecord), use code like this:
var predicate = /* Build up predicate with PredicateBuilder */;
var recs = SubsonicClass.All().Where(predicate.Compile()).ToList();

Resources