0

I was working on an entity framework project and I wanted to do a LINQ Where() clause that has a contains based on the string state of an entity passed in as an enum.

string strState = state.ToString(); //convert enum to string
var logs = await _context.DispatchChangeLogs.Where(x => x.NewValues.Contains(strState)).ToListAsync();

This returns the error:

Argument data type text is invalid for argument 1 of charindex function.

In the above example, the strState value is "Approved". When I put "Approved" directly into the Contains method, it returns that set of values I am looking for with this query.

I found this article that gives a good explanation on the issue.

I changed the NewValues type in the database to varchar(max) and nvarchar(max) as a test. I updated my VS database project and republished to my local database. The database shows it as nvarchar(max).

enter image description here

The target framework for the DB project is .Net Framework 4.5.2

Any thoughts on why this would still be returning that "data type text" error?

NetMage
  • 26,163
  • 3
  • 34
  • 55
  • "This returns the error" - where, how? Is this a SQL error or a EF error? – NetMage Apr 06 '23 at 23:16
  • I apologize for the late response. I believe the error is a SQL error, but I think it is caused by EF. I have tried changing it to several different BD types, all to no avail. I found a workaround, but still want to know what is causing this. – crmcode2012 Apr 27 '23 at 13:31
  • Argument 1 of `CHARINDEX` is the text to find, so in your case the value of `strState` is being converted into `text` type by EF for some reason. Can you capture/output the SQL for your query? I assume you are using EF 4.8? – NetMage Apr 27 '23 at 19:12

0 Answers0