title: String or binary data would be truncated WHERE IN
body: <p>I am getting SQL Exception:</p>
  
  <blockquote>
    <p>String or binary data would be truncated</p>
  </blockquote>
  
  <p>in SELECT. I read few questions with same title but they were all about inserting. I am SELECTING.</p>
  
  <p>Code is following:</p>
  
  <pre><code>List&lt;CategoryName&gt; navigation = await db.Query&lt;CategoryName&gt;().FromSql(
      $"WITH NestedCategories AS (
          SELECT *
          FROM Categories
          WHERE Id IN (
              {string.Join(",", products.Select(x =&gt;
                  x.Categories.First().CategoryId).Distinct().Select(x =&gt; $"'{x}'"))}
          )
          UNION ALL 
              SELECT t.*
              FROM Categories t
              INNER JOIN NestedCategories c On c.ParentId = t.Id
      )
      SELECT DISTINCT c.Id, c.Name, c.ParentId
      FROM NestedCategories c")
  .AsNoTracking()
  .ToListAsync();
  </code></pre>
  
  <p>If I generate <strong>string.Join</strong> to console and then put SQL command into query window in Management Studio I dont get any error. I get proper results. Issue is obviously in EF CORE that I am passing too many category Ids. Command is to get nesting categories based on Product-Category Id.</p>
  
  <p>EDIT:</p>
  
  <pre><code>public class CategoryName
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public int? ParentId { get; set; }
  }
  </code></pre>
  
  <p>EDIT 2 - Solution</p>
  
  <pre><code>string inClause = string.Join(",", products.Select(x =&gt; x.Categories.First().CategoryId).Distinct().Select(x =&gt; $"'{x}'"));
  
  List&lt;CategoryName&gt; navigation = new List&lt;CategoryName&gt;();
  
  using (DbCommand command = db.Database.GetDbConnection().CreateCommand())
  {
       command.CommandText = $"WITH NestedCategories AS (SELECT * FROM Categories WHERE Id IN ({inClause}) UNION ALL SELECT t.* FROM Categories t INNER JOIN NestedCategories c On c.ParentId = t.Id) SELECT DISTINCT c.Id, c.Name, c.ParentId FROM NestedCategories c";
  
        await db.Database.GetDbConnection().OpenAsync();
  
        DbDataReader reader = await command.ExecuteReaderAsync();
  
        while (await reader.ReadAsync())
            navigation.Add(new CategoryName() { Id = reader.GetInt32(0), Name = reader.GetString(1), ParentId = await reader.IsDBNullAsync(2) ? null : await reader.GetFieldValueAsync&lt;int?&gt;(2) });
  }
  </code></pre>