0

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.

2
  • try new int[] { pid = ImId }. You create a dynamic type when you do new { pid = ImId } or make item.catIDs a dynamic[] Commented Dec 9, 2015 at 15:53
  • or is there not con.Query<int>? Commented Dec 9, 2015 at 15:54

2 Answers 2

1

Instead of using

con.Query(...

try using

con.Query<int>(...
Sign up to request clarification or add additional context in comments.

Comments

0

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)
{

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.