C# Linq - Cannot implicitly convert IEnumerable to List












16















I have a List defined like this :



public List<string> AttachmentURLS;


I am adding items to the list like this:



instruction.AttachmentURLS = curItem.Attributes["ows_Attachments"].Value.Split(';').ToList().Where(Attachment => !String.IsNullOrEmpty(Attachment));


But I am getting this error: Cannot implicitly convert IEnumerable to List



What am I doing wrong?










share|improve this question























  • Did you try adding a type cast? instruction.AttachmentURLS = (List<type, type>)curItem.Attributes... Oh of course, I forgot about the .ToList() method. Just add that to the end.

    – Nilbert
    May 17 '10 at 23:31


















16















I have a List defined like this :



public List<string> AttachmentURLS;


I am adding items to the list like this:



instruction.AttachmentURLS = curItem.Attributes["ows_Attachments"].Value.Split(';').ToList().Where(Attachment => !String.IsNullOrEmpty(Attachment));


But I am getting this error: Cannot implicitly convert IEnumerable to List



What am I doing wrong?










share|improve this question























  • Did you try adding a type cast? instruction.AttachmentURLS = (List<type, type>)curItem.Attributes... Oh of course, I forgot about the .ToList() method. Just add that to the end.

    – Nilbert
    May 17 '10 at 23:31
















16












16








16


2






I have a List defined like this :



public List<string> AttachmentURLS;


I am adding items to the list like this:



instruction.AttachmentURLS = curItem.Attributes["ows_Attachments"].Value.Split(';').ToList().Where(Attachment => !String.IsNullOrEmpty(Attachment));


But I am getting this error: Cannot implicitly convert IEnumerable to List



What am I doing wrong?










share|improve this question














I have a List defined like this :



public List<string> AttachmentURLS;


I am adding items to the list like this:



instruction.AttachmentURLS = curItem.Attributes["ows_Attachments"].Value.Split(';').ToList().Where(Attachment => !String.IsNullOrEmpty(Attachment));


But I am getting this error: Cannot implicitly convert IEnumerable to List



What am I doing wrong?







c# linq






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 17 '10 at 23:28









JL.JL.

31.3k106275421




31.3k106275421













  • Did you try adding a type cast? instruction.AttachmentURLS = (List<type, type>)curItem.Attributes... Oh of course, I forgot about the .ToList() method. Just add that to the end.

    – Nilbert
    May 17 '10 at 23:31





















  • Did you try adding a type cast? instruction.AttachmentURLS = (List<type, type>)curItem.Attributes... Oh of course, I forgot about the .ToList() method. Just add that to the end.

    – Nilbert
    May 17 '10 at 23:31



















Did you try adding a type cast? instruction.AttachmentURLS = (List<type, type>)curItem.Attributes... Oh of course, I forgot about the .ToList() method. Just add that to the end.

– Nilbert
May 17 '10 at 23:31







Did you try adding a type cast? instruction.AttachmentURLS = (List<type, type>)curItem.Attributes... Oh of course, I forgot about the .ToList() method. Just add that to the end.

– Nilbert
May 17 '10 at 23:31














3 Answers
3






active

oldest

votes


















38














The Where method returns an IEnumerable<T>. Try adding



.ToList()


to the end like so:



instruction.AttachmentURLS = curItem.Attributes["ows_Attachments"]
.Value.Split(';').ToList()
.Where(Attachment => !String.IsNullOrEmpty(Attachment))
.ToList();





share|improve this answer


























  • I guess that means I can remove my existing .ToList from the middle of the statement?

    – JL.
    May 17 '10 at 23:31






  • 4





    Yes, you can remove it.

    – smoak
    May 17 '10 at 23:38











  • Got this error, google'd it, apparently I've already upvoted it and been here before, I need more caffeine.

    – PurpleSmurph
    Sep 22 '17 at 10:56



















7














Move the .ToList() to the end like this



instruction.AttachmentURLS = curItem
.Attributes["ows_Attachments"]
.Value
.Split(';')
.Where(Attachment => !String.IsNullOrEmpty(Attachment))
.ToList();


The Where extension method returns IEnumerable<string> and Where will work on arrays, so the ToList isn't needed after the Split.






share|improve this answer

































    2














    The .ToList() should be at last. Because in your code you perform the .ToList() operation earlier and after that again it goes to previous state. The Where method returns an IEnumerable.






    share|improve this answer



















    • 7





      why do you give the same answer eight hours after someone else did it?

      – Oliver
      May 18 '10 at 8:08











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2853647%2fc-sharp-linq-cannot-implicitly-convert-ienumerablestring-to-liststring%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    38














    The Where method returns an IEnumerable<T>. Try adding



    .ToList()


    to the end like so:



    instruction.AttachmentURLS = curItem.Attributes["ows_Attachments"]
    .Value.Split(';').ToList()
    .Where(Attachment => !String.IsNullOrEmpty(Attachment))
    .ToList();





    share|improve this answer


























    • I guess that means I can remove my existing .ToList from the middle of the statement?

      – JL.
      May 17 '10 at 23:31






    • 4





      Yes, you can remove it.

      – smoak
      May 17 '10 at 23:38











    • Got this error, google'd it, apparently I've already upvoted it and been here before, I need more caffeine.

      – PurpleSmurph
      Sep 22 '17 at 10:56
















    38














    The Where method returns an IEnumerable<T>. Try adding



    .ToList()


    to the end like so:



    instruction.AttachmentURLS = curItem.Attributes["ows_Attachments"]
    .Value.Split(';').ToList()
    .Where(Attachment => !String.IsNullOrEmpty(Attachment))
    .ToList();





    share|improve this answer


























    • I guess that means I can remove my existing .ToList from the middle of the statement?

      – JL.
      May 17 '10 at 23:31






    • 4





      Yes, you can remove it.

      – smoak
      May 17 '10 at 23:38











    • Got this error, google'd it, apparently I've already upvoted it and been here before, I need more caffeine.

      – PurpleSmurph
      Sep 22 '17 at 10:56














    38












    38








    38







    The Where method returns an IEnumerable<T>. Try adding



    .ToList()


    to the end like so:



    instruction.AttachmentURLS = curItem.Attributes["ows_Attachments"]
    .Value.Split(';').ToList()
    .Where(Attachment => !String.IsNullOrEmpty(Attachment))
    .ToList();





    share|improve this answer















    The Where method returns an IEnumerable<T>. Try adding



    .ToList()


    to the end like so:



    instruction.AttachmentURLS = curItem.Attributes["ows_Attachments"]
    .Value.Split(';').ToList()
    .Where(Attachment => !String.IsNullOrEmpty(Attachment))
    .ToList();






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 18:16









    kmote

    10.9k85176




    10.9k85176










    answered May 17 '10 at 23:30









    smoaksmoak

    9,55752633




    9,55752633













    • I guess that means I can remove my existing .ToList from the middle of the statement?

      – JL.
      May 17 '10 at 23:31






    • 4





      Yes, you can remove it.

      – smoak
      May 17 '10 at 23:38











    • Got this error, google'd it, apparently I've already upvoted it and been here before, I need more caffeine.

      – PurpleSmurph
      Sep 22 '17 at 10:56



















    • I guess that means I can remove my existing .ToList from the middle of the statement?

      – JL.
      May 17 '10 at 23:31






    • 4





      Yes, you can remove it.

      – smoak
      May 17 '10 at 23:38











    • Got this error, google'd it, apparently I've already upvoted it and been here before, I need more caffeine.

      – PurpleSmurph
      Sep 22 '17 at 10:56

















    I guess that means I can remove my existing .ToList from the middle of the statement?

    – JL.
    May 17 '10 at 23:31





    I guess that means I can remove my existing .ToList from the middle of the statement?

    – JL.
    May 17 '10 at 23:31




    4




    4





    Yes, you can remove it.

    – smoak
    May 17 '10 at 23:38





    Yes, you can remove it.

    – smoak
    May 17 '10 at 23:38













    Got this error, google'd it, apparently I've already upvoted it and been here before, I need more caffeine.

    – PurpleSmurph
    Sep 22 '17 at 10:56





    Got this error, google'd it, apparently I've already upvoted it and been here before, I need more caffeine.

    – PurpleSmurph
    Sep 22 '17 at 10:56













    7














    Move the .ToList() to the end like this



    instruction.AttachmentURLS = curItem
    .Attributes["ows_Attachments"]
    .Value
    .Split(';')
    .Where(Attachment => !String.IsNullOrEmpty(Attachment))
    .ToList();


    The Where extension method returns IEnumerable<string> and Where will work on arrays, so the ToList isn't needed after the Split.






    share|improve this answer






























      7














      Move the .ToList() to the end like this



      instruction.AttachmentURLS = curItem
      .Attributes["ows_Attachments"]
      .Value
      .Split(';')
      .Where(Attachment => !String.IsNullOrEmpty(Attachment))
      .ToList();


      The Where extension method returns IEnumerable<string> and Where will work on arrays, so the ToList isn't needed after the Split.






      share|improve this answer




























        7












        7








        7







        Move the .ToList() to the end like this



        instruction.AttachmentURLS = curItem
        .Attributes["ows_Attachments"]
        .Value
        .Split(';')
        .Where(Attachment => !String.IsNullOrEmpty(Attachment))
        .ToList();


        The Where extension method returns IEnumerable<string> and Where will work on arrays, so the ToList isn't needed after the Split.






        share|improve this answer















        Move the .ToList() to the end like this



        instruction.AttachmentURLS = curItem
        .Attributes["ows_Attachments"]
        .Value
        .Split(';')
        .Where(Attachment => !String.IsNullOrEmpty(Attachment))
        .ToList();


        The Where extension method returns IEnumerable<string> and Where will work on arrays, so the ToList isn't needed after the Split.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 28 '15 at 11:49

























        answered May 17 '10 at 23:30









        juharrjuharr

        25.4k33676




        25.4k33676























            2














            The .ToList() should be at last. Because in your code you perform the .ToList() operation earlier and after that again it goes to previous state. The Where method returns an IEnumerable.






            share|improve this answer



















            • 7





              why do you give the same answer eight hours after someone else did it?

              – Oliver
              May 18 '10 at 8:08
















            2














            The .ToList() should be at last. Because in your code you perform the .ToList() operation earlier and after that again it goes to previous state. The Where method returns an IEnumerable.






            share|improve this answer



















            • 7





              why do you give the same answer eight hours after someone else did it?

              – Oliver
              May 18 '10 at 8:08














            2












            2








            2







            The .ToList() should be at last. Because in your code you perform the .ToList() operation earlier and after that again it goes to previous state. The Where method returns an IEnumerable.






            share|improve this answer













            The .ToList() should be at last. Because in your code you perform the .ToList() operation earlier and after that again it goes to previous state. The Where method returns an IEnumerable.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 18 '10 at 7:46









            JohnnyJohnny

            1,19231122




            1,19231122








            • 7





              why do you give the same answer eight hours after someone else did it?

              – Oliver
              May 18 '10 at 8:08














            • 7





              why do you give the same answer eight hours after someone else did it?

              – Oliver
              May 18 '10 at 8:08








            7




            7





            why do you give the same answer eight hours after someone else did it?

            – Oliver
            May 18 '10 at 8:08





            why do you give the same answer eight hours after someone else did it?

            – Oliver
            May 18 '10 at 8:08


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2853647%2fc-sharp-linq-cannot-implicitly-convert-ienumerablestring-to-liststring%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine