problem with linq equivalent of sql in clause

Thread is closed for posting
3 posts, 0 answers
  1. Jeff Clark
    Jeff Clark avatar
    15 posts
    Member since:
    Feb 2010

    Posted 21 Sep 2011 Link to this post

     I am getting the following runtime error when executing a linq query that uses a list as the equivalent to the sql in clause

    Object of type

    'Telerik.OpenAccess.Query.Piece`1[<>f__AnonymousType17`6[System.Int32,System.String,System.String,System.String,System.String,System.String]]' cannot be converted to type 'Telerik.OpenAccess.Query.Piece`1[System.Int32]'.

    Here is my code:
    IList<int> equipList = new List<int>();
    foreach (RadListBoxItem item in SelectedEquipment_LB.Items)
    var query = from eq in dbContext.InventoryVwEquipments
                where equipList.Contains(eq.EquipId)
                select new { eq.EquipId, eq.EquipmentName, eq.Company, eq.Series, eq.Model, eq.Serial };
    RadListView1.DataSource = query;

    Here is the sql code generated by linq query.

    SELECT a.[EquipId] AS COL1, a.[EquipmentName] AS COL2, a.[Company] AS COL3, a.[Series] AS COL4, a.[Model] AS COL5, a.[Serial] AS COL6 FROM [Inventory].[vwEquipment] a WHERE (a.[EquipId] IN (?,?,?,?,?,?,?,?,?,?,?,?))

    Any ideas why the generated sql query contains question marks instead of the integers from the equipList list?

    Thanks for your help.
  2. Jeff Clark
    Jeff Clark avatar
    15 posts
    Member since:
    Feb 2010

    Posted 26 Sep 2011 Link to this post

    Never mind.  I figured it out. It seems I have to explicitly execute the quey instead of letting the control execute the query. Changing RadListView1.DataSource = query;   to  RadListView1.DataSource = query.ToList();  gets rid of the error and the control has the expected data. The question marks in the SQL code was not the issue as I was suspecting. The SQL code here is what I get with the query.ToString() command, but that is not the exact SQL command that is sent to the SQL server.
  3. Thomas
    Thomas avatar
    590 posts

    Posted 27 Sep 2011 Link to this post

    Hello Jeff,

    yes, the ToString() of the query does not produce the real sql and the parameters are not in @p0 etc form, but abstract. Also, the execution is always lazy with LINQ, and a ToList() gives you the ability to explicitly say- hey, I need the data now.

    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

Back to Top