This is a migrated thread and some comments may be shown as answers.

problem with linq equivalent of sql in clause

2 Answers 110 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jeff Clark
Top achievements
Rank 1
Jeff Clark asked on 21 Sep 2011, 06:50 PM
 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)
{
    equipList.Add(Convert.ToInt32(item.Value));
}
 
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 Answers, 1 is accepted

Sort by
0
Jeff Clark
Top achievements
Rank 1
answered on 26 Sep 2011, 10:15 PM
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.
0
Thomas
Telerik team
answered on 27 Sep 2011, 05:19 PM
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.

Regards,
Thomas
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 >>

Tags
LINQ (LINQ specific questions)
Asked by
Jeff Clark
Top achievements
Rank 1
Answers by
Jeff Clark
Top achievements
Rank 1
Thomas
Telerik team
Share this question
or