dmikester1
dmikester1

Reputation: 1362

linq mysql query into int array

I'm still very new in my linq skills. I have a mysql query that returns 3 records, each record is an int. I want to stick those ints into an array. I thought I could make it easy on myself and do that with a linq command instead of creating a reader and looping through the results. Here is my code:

query = "SELECT cic.catid FROM cart_item ci LEFT JOIN cart_item_category cic USING (itemref) WHERE ci.pid = @pid";

try
{
    item.catIDs = con.Query(query, new { pid = ImId}).ToArray();
}
catch(MySqlException ex)
{

}

I am getting the error: Cannot implicitly convert type 'dynamic[]' to 'int[]'

I'm assuming my linq query is not correct.

Upvotes: 0

Views: 81

Answers (2)

Avinash Jain
Avinash Jain

Reputation: 7616

Look like catIds is array of integer, But you are trying to assign anonymous collection to integer which is not correct.

Please try the below code. I think it should work

query = "SELECT cic.catid FROM cart_item ci LEFT JOIN cart_item_category cic USING (itemref) WHERE ci.pid = @pid";

try
{
    item.catIDs = con.Query(query, Convert.ToInt32(ImId)).ToArray();
}
catch(MySqlException ex)
{

}

Upvotes: 0

Lewis Taylor
Lewis Taylor

Reputation: 728

Instead of using

con.Query(...

try using

con.Query<int>(...

Upvotes: 1

Related Questions