none
Problem convert result to List<int> RRS feed

  • Question

  • The following code got an error, it said it cannot convert to List<int>

    List<int> CustList = (from c in context.Customers where c.CustomerId == 1 || c.CustomerId == 2 select new {c.CustomerId}).ToList();

    Why, how can I fox it?

    Thanks

    Wednesday, September 14, 2011 6:28 AM

Answers

  • Anyway,

    You could perform it in this way if it is an int type:

    List<int> CustList = (from c in context.Customers where c.CustomerId == 1 || c.CustomerId == 2 select c.CustomerId).ToList();
    

    As you tried to do it, you were building a new object, so it results in an anonymous type.

    Best regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    • Marked as answer by peter 9 Wednesday, September 14, 2011 11:28 PM
    Wednesday, September 14, 2011 10:51 AM
  • Hi Peter;

    The issue with your code is that in the Linq query when you use the new keyword in the select statement as you did it will return a Anonymous type and because the Anonymous type is NOT an int you get the error. Because you are only returning one item for each Customer that is filtered by the where clause you can remove the new { and } leaving only the c.CustomerId and all should work fine because you will now be returning an Enumerable of int's which can be made into a List<int>.

     

    List<int> CustList = (from c in context.Customers 
                          where c.CustomerId == 1 || c.CustomerId == 2 
                          select c.CustomerId).ToList();

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Edited by Fernando Soto - MCSD Wednesday, September 14, 2011 2:35 PM
    • Marked as answer by peter 9 Wednesday, September 14, 2011 11:28 PM
    Wednesday, September 14, 2011 2:32 PM

All replies

  • Hi,

    First of all, is your CustomerId property of type int? or is it an string property?

    Regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Wednesday, September 14, 2011 10:46 AM
  • Anyway,

    You could perform it in this way if it is an int type:

    List<int> CustList = (from c in context.Customers where c.CustomerId == 1 || c.CustomerId == 2 select c.CustomerId).ToList();
    

    As you tried to do it, you were building a new object, so it results in an anonymous type.

    Best regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    • Marked as answer by peter 9 Wednesday, September 14, 2011 11:28 PM
    Wednesday, September 14, 2011 10:51 AM
  • Hi Peter;

    The issue with your code is that in the Linq query when you use the new keyword in the select statement as you did it will return a Anonymous type and because the Anonymous type is NOT an int you get the error. Because you are only returning one item for each Customer that is filtered by the where clause you can remove the new { and } leaving only the c.CustomerId and all should work fine because you will now be returning an Enumerable of int's which can be made into a List<int>.

     

    List<int> CustList = (from c in context.Customers 
                          where c.CustomerId == 1 || c.CustomerId == 2 
                          select c.CustomerId).ToList();

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Edited by Fernando Soto - MCSD Wednesday, September 14, 2011 2:35 PM
    • Marked as answer by peter 9 Wednesday, September 14, 2011 11:28 PM
    Wednesday, September 14, 2011 2:32 PM
  • Hi Fernandoo,

    Thank u for the help.

    You said "Because you are only returning one item for each Customer that is filtered by the where clause you can remove the new { and } leaving only the c.CustomerId". Does this mean that if returning more than one item, then I need use new keyword?

    Wednesday, September 14, 2011 11:31 PM
  • Hi Peter;

    To your question, "Does this mean that if returning more than one item, then I need use new keyword?", Yes, with a single item in the select clause the results is of the same type as the item in the select clause. If you have multiple items in the select then it is a complex type and it must be given a type or defined as a Anonymous type.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, September 15, 2011 3:22 PM