开发者

how to compare string in lambda expression

I have an issue that I would appreciate any help with.

Background - I have a database that holds a string (i.e. - StackOverFlow) and I need to compare a passed in value to that string and make it case sensitive.

Right now I have a connection to the database (using EF 4), an IRepo<T> interface (which I use to access the table) and a FindAllMatching method that returns an IQueryable<table> and takes in an Expression<Func<tableName开发者_JAVA百科, bool>> expression parameter.

Repo class:

public class TableRepo : IRepo<table>{
  private EF4Entity _dc = new EF4Entity();
  public IQueryable<table> FindAllMatching(Expression<Func<table, bool>> expression)
  {
    return _dc.table.Where(expression);
  }
}

Calling Code:

public class CallingClass
{
  public void CallingMethod(string enteredStringFromTextBoxOnFrontEnd)
  {
    var tableDB = new TableRepo();
    var returnValue = tableDB.FindAllMatching(x => x.ColumnInTableThatHoldsString.CompareTo(enteredStringFromTextBoxOnFrontEnd) ==0);
  }
}

Right now, when I pass in a string that matches what is in the table in every way but in case (stackoverflow as opposed to StackOverFlow) the returnValue actually returns a row from the table. If the value that is passed in doesn't match the value contained in the database table exactly, I want it to return null or a count of zero.

I have tried doing .Equals(enteredStringFromTextBoxOnFrontEnd, StringComparison.OrdinalIgnoreCase) and all the other StringComparison values and nothing compares case.

If you need more information please don't hesitate to ask.

Thank you,

Tim


Linq To Sql uses the database compare rules.

You need to add an extra IEnumerable.Where at the end to do a local filtering of the result

var returnValue = (ItableDB.FindAllMatching(x => x.ColumnInTableThatHoldsString == enteredStringFromTextBoxOnFrontEnd) as IEnumerble<TableRepo>).Where(x => x.ColumnInTableThatHoldsString == enteredStringFromTextBoxOnFrontEnd);


I think you'll find that this is not an issue with your code but with your database. I'm presuming that you're using some flavour of SQL Server. By default when you install SQL Server it is case insensitive on all text queries.

You might want to see this question for possible answers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜