Comparison operators are not supported for type 'System.String []'

why is this line:

var category = _dataContext.Categories.Where<Category>(p => p.Keywords.Split(' ').Contains<string>(context.Request.QueryString["q"])).First();

throws a System.NotSupportedException exception:

Comparison operators are not supported for type 'System.String []'

And how can I fix this? Thanks.

+3
source share
5 answers

So, are you looking for a value (from the query string) in a column with space separators in the database? And do you use Splitto query individual values ​​inside the database?

(just checking my assumptions ...)

string.Splitnot supported this way (in a database based on column data) - see here supported row operations . (note that this is string.Splitclearly not supported).

; ( ), ; :

string searchFor = DELIMITER + searchValue + DELIMITER;
...
.Where(row => row.Value.Contains(searchFor));

; , UDF, varchar ( / ) UDF - :

.Where(row => ctx.ContainsValue(row.Value, searchValue)); // ContainsValue is our UDF

- ...

.Where(row => row.Values.Any(s=>s.Value == searchValue));
+4

string.split LINQ-to-SQL.

. . .

var category = 
    _dataContext.Categories.ToList()
    .Where<Category>(p => p.Keywords.Split(' ').Contains<string>(context.Request.QueryString["q"])).First();

.ToList() , .

+3

, context.Request.QueryString["q"] . , URL- .

, q, : context.Request.QueryString["q"].SingleOrDefault().

0

-, . , .. .

, , , .

  • UDF , SQL Server :

CREATE FUNCTION FN_CHARLIST_TO_TABLE
 (@list      nvarchar(MAX),
  @delimiter nchar(1) = N',')
RETURNS @tbl TABLE (listpos int IDENTITY(1, 1) NOT NULL,
              str     varchar(4000)      NOT NULL,
              nstr    nvarchar(2000)     NOT NULL) AS
/*                
  Comments:
        - Takes a CSV string, and creates a table of data from this
        - Each item takes one row in the following format
            listpos - the index of the item in the string (effectively a row number in the output)
            str - The value, as VARCHAR
            nstr - The value, as NVARCHAR

        - This function is a direct extract from http://www.sommarskog.se/arrays-in-sql-2005.html#iter-list-of-strings
Usage:
    SELECT * 
    FROM  t 
    JOIN FN_CHARLIST_TO_TABLE('a,b,c,1,2,3', ',') list
    ON t.Name = list.str        
*/
BEGIN
   DECLARE @endpos   int,
       @startpos int,
       @textpos  int,
       @chunklen smallint,
       @tmpstr   nvarchar(4000),
       @leftover nvarchar(4000),
       @tmpval   nvarchar(4000)

   SET @textpos = 1
   SET @leftover = ''
   WHILE @textpos  0
      BEGIN
     SET @tmpval = ltrim(rtrim(substring(@tmpstr, @startpos + 1,
                         @endpos - @startpos - 1)))
     INSERT @tbl (str, nstr) VALUES(@tmpval, @tmpval)
     SET @startpos = @endpos
     SET @endpos = charindex(@delimiter COLLATE Slovenian_BIN2,
                 @tmpstr, @startpos + 1)
      END

      SET @leftover = right(@tmpstr, datalength(@tmpstr) / 2 - @startpos)
   END

   INSERT @tbl(str, nstr)
      VALUES (ltrim(rtrim(@leftover)), ltrim(rtrim(@leftover)))
   RETURN
END

  1. DBML .
  2. , UDF

var catergories = from cat in _datacontext.Categories
                  from keyword in _datacontext.FN_CHARLIST_TO_TABLE(cat.Keywords, ' ')
                  where keyword.str == context.Request.QueryString["q"]
                  select cat;

.

0

1) ToList() var

2) 1 :

-2

Source: https://habr.com/ru/post/1714049/


All Articles