C# linq Select objects in list looking inside another list of objects





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







4















I have a class like this:



public class MailMessage
{
public string From {get; set; };
public string To {get; set; };
public ICollection<Attachment> Attachments { get; set; }
}

public class Attachment
{
public string Name {get; set;}
public byte Content {get;set;}

}


I would like to get all attachments in Attachments collection whose name ends with .pdf.



I tried the following:



List<MailMessage> list = new List<MailMessage>();
List<attachment> pdfAttachmentsCollection = list.Where(x => x.Attachments
.Where(attachment =>
attachment.Name.EndsWith(".pdf")).Select(y => y.Content));


However, this doesnt work. Any suggestions. It gives error:




Cannot implicitly convert type
'System.Collections.Generic.IEnumerable ' to 'bool'











share|improve this question




















  • 1





    Possible duplicate of Difference Between Select and SelectMany

    – mjwills
    Nov 23 '18 at 4:02






  • 1





    That error is probably coming from the first where since it isn't a boolean expression for the predicate that is given.

    – Jonathan Van Dam
    Nov 23 '18 at 4:09






  • 1





    var pdfAttachmentsCollection = list.SelectMany(x => x.Attachments.Where(a => a.Name.ToLower().EndsWith(".pdf"))).ToList();

    – Jimi
    Nov 23 '18 at 4:11








  • 1





    I'd suggest starting with list.SelectMany(x => x.Attachments). The rest is standard Where stuff that you'll work out easily. Jim's approach will work equally well.

    – mjwills
    Nov 23 '18 at 4:12








  • 1





    Better for case-insensitive check: Name.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase)

    – gregmac
    Nov 23 '18 at 4:21


















4















I have a class like this:



public class MailMessage
{
public string From {get; set; };
public string To {get; set; };
public ICollection<Attachment> Attachments { get; set; }
}

public class Attachment
{
public string Name {get; set;}
public byte Content {get;set;}

}


I would like to get all attachments in Attachments collection whose name ends with .pdf.



I tried the following:



List<MailMessage> list = new List<MailMessage>();
List<attachment> pdfAttachmentsCollection = list.Where(x => x.Attachments
.Where(attachment =>
attachment.Name.EndsWith(".pdf")).Select(y => y.Content));


However, this doesnt work. Any suggestions. It gives error:




Cannot implicitly convert type
'System.Collections.Generic.IEnumerable ' to 'bool'











share|improve this question




















  • 1





    Possible duplicate of Difference Between Select and SelectMany

    – mjwills
    Nov 23 '18 at 4:02






  • 1





    That error is probably coming from the first where since it isn't a boolean expression for the predicate that is given.

    – Jonathan Van Dam
    Nov 23 '18 at 4:09






  • 1





    var pdfAttachmentsCollection = list.SelectMany(x => x.Attachments.Where(a => a.Name.ToLower().EndsWith(".pdf"))).ToList();

    – Jimi
    Nov 23 '18 at 4:11








  • 1





    I'd suggest starting with list.SelectMany(x => x.Attachments). The rest is standard Where stuff that you'll work out easily. Jim's approach will work equally well.

    – mjwills
    Nov 23 '18 at 4:12








  • 1





    Better for case-insensitive check: Name.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase)

    – gregmac
    Nov 23 '18 at 4:21














4












4








4








I have a class like this:



public class MailMessage
{
public string From {get; set; };
public string To {get; set; };
public ICollection<Attachment> Attachments { get; set; }
}

public class Attachment
{
public string Name {get; set;}
public byte Content {get;set;}

}


I would like to get all attachments in Attachments collection whose name ends with .pdf.



I tried the following:



List<MailMessage> list = new List<MailMessage>();
List<attachment> pdfAttachmentsCollection = list.Where(x => x.Attachments
.Where(attachment =>
attachment.Name.EndsWith(".pdf")).Select(y => y.Content));


However, this doesnt work. Any suggestions. It gives error:




Cannot implicitly convert type
'System.Collections.Generic.IEnumerable ' to 'bool'











share|improve this question
















I have a class like this:



public class MailMessage
{
public string From {get; set; };
public string To {get; set; };
public ICollection<Attachment> Attachments { get; set; }
}

public class Attachment
{
public string Name {get; set;}
public byte Content {get;set;}

}


I would like to get all attachments in Attachments collection whose name ends with .pdf.



I tried the following:



List<MailMessage> list = new List<MailMessage>();
List<attachment> pdfAttachmentsCollection = list.Where(x => x.Attachments
.Where(attachment =>
attachment.Name.EndsWith(".pdf")).Select(y => y.Content));


However, this doesnt work. Any suggestions. It gives error:




Cannot implicitly convert type
'System.Collections.Generic.IEnumerable ' to 'bool'








c# linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 5:44







Gauravsa

















asked Nov 23 '18 at 3:58









GauravsaGauravsa

3,6271919




3,6271919








  • 1





    Possible duplicate of Difference Between Select and SelectMany

    – mjwills
    Nov 23 '18 at 4:02






  • 1





    That error is probably coming from the first where since it isn't a boolean expression for the predicate that is given.

    – Jonathan Van Dam
    Nov 23 '18 at 4:09






  • 1





    var pdfAttachmentsCollection = list.SelectMany(x => x.Attachments.Where(a => a.Name.ToLower().EndsWith(".pdf"))).ToList();

    – Jimi
    Nov 23 '18 at 4:11








  • 1





    I'd suggest starting with list.SelectMany(x => x.Attachments). The rest is standard Where stuff that you'll work out easily. Jim's approach will work equally well.

    – mjwills
    Nov 23 '18 at 4:12








  • 1





    Better for case-insensitive check: Name.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase)

    – gregmac
    Nov 23 '18 at 4:21














  • 1





    Possible duplicate of Difference Between Select and SelectMany

    – mjwills
    Nov 23 '18 at 4:02






  • 1





    That error is probably coming from the first where since it isn't a boolean expression for the predicate that is given.

    – Jonathan Van Dam
    Nov 23 '18 at 4:09






  • 1





    var pdfAttachmentsCollection = list.SelectMany(x => x.Attachments.Where(a => a.Name.ToLower().EndsWith(".pdf"))).ToList();

    – Jimi
    Nov 23 '18 at 4:11








  • 1





    I'd suggest starting with list.SelectMany(x => x.Attachments). The rest is standard Where stuff that you'll work out easily. Jim's approach will work equally well.

    – mjwills
    Nov 23 '18 at 4:12








  • 1





    Better for case-insensitive check: Name.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase)

    – gregmac
    Nov 23 '18 at 4:21








1




1





Possible duplicate of Difference Between Select and SelectMany

– mjwills
Nov 23 '18 at 4:02





Possible duplicate of Difference Between Select and SelectMany

– mjwills
Nov 23 '18 at 4:02




1




1





That error is probably coming from the first where since it isn't a boolean expression for the predicate that is given.

– Jonathan Van Dam
Nov 23 '18 at 4:09





That error is probably coming from the first where since it isn't a boolean expression for the predicate that is given.

– Jonathan Van Dam
Nov 23 '18 at 4:09




1




1





var pdfAttachmentsCollection = list.SelectMany(x => x.Attachments.Where(a => a.Name.ToLower().EndsWith(".pdf"))).ToList();

– Jimi
Nov 23 '18 at 4:11







var pdfAttachmentsCollection = list.SelectMany(x => x.Attachments.Where(a => a.Name.ToLower().EndsWith(".pdf"))).ToList();

– Jimi
Nov 23 '18 at 4:11






1




1





I'd suggest starting with list.SelectMany(x => x.Attachments). The rest is standard Where stuff that you'll work out easily. Jim's approach will work equally well.

– mjwills
Nov 23 '18 at 4:12







I'd suggest starting with list.SelectMany(x => x.Attachments). The rest is standard Where stuff that you'll work out easily. Jim's approach will work equally well.

– mjwills
Nov 23 '18 at 4:12






1




1





Better for case-insensitive check: Name.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase)

– gregmac
Nov 23 '18 at 4:21





Better for case-insensitive check: Name.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase)

– gregmac
Nov 23 '18 at 4:21












1 Answer
1






active

oldest

votes


















3














A couple issues:




  • You need to SelectMany from the MailMessage object to get all attachments in a single collection

  • You're selecting y.Content which is byte but trying to get a list of Attachment

  • LINQ returns IEnumerables, not Lists


Rewritten, you probably want to do something like:



List<MailMessage> list = new List<MailMessage>();
IEnumerable<Attachment> pdfAttachmentsCollection =
list.SelectMany(message =>
message.Attachments
.Where(attachment => attachment.Name.EndsWith(".pdf")));


This is also a good candidate to write as a query expression:



IEnumerable<Attachment> pdfAttachmentsCollection = 
from message in list
from attachment in message.Attachments
where attachment.Name.EndsWith(".pdf")
select attachment;


Both of these do the same thing, just with different syntax.



If you want this as a List<Attachment> you can also do .ToList() at the end of either query. As an IEnumerable, it won't be evaluated until you start iterating through it (and depending on how you use it, you might never have the full collection in memory at any time). However, if you do multiple iterations or operations on it, it'll cause it to be evaluated multiple times -- so in those cases .ToList() makes sense for sure.






share|improve this answer
























    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%2f53440492%2fc-sharp-linq-select-objects-in-list-looking-inside-another-list-of-objects%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    A couple issues:




    • You need to SelectMany from the MailMessage object to get all attachments in a single collection

    • You're selecting y.Content which is byte but trying to get a list of Attachment

    • LINQ returns IEnumerables, not Lists


    Rewritten, you probably want to do something like:



    List<MailMessage> list = new List<MailMessage>();
    IEnumerable<Attachment> pdfAttachmentsCollection =
    list.SelectMany(message =>
    message.Attachments
    .Where(attachment => attachment.Name.EndsWith(".pdf")));


    This is also a good candidate to write as a query expression:



    IEnumerable<Attachment> pdfAttachmentsCollection = 
    from message in list
    from attachment in message.Attachments
    where attachment.Name.EndsWith(".pdf")
    select attachment;


    Both of these do the same thing, just with different syntax.



    If you want this as a List<Attachment> you can also do .ToList() at the end of either query. As an IEnumerable, it won't be evaluated until you start iterating through it (and depending on how you use it, you might never have the full collection in memory at any time). However, if you do multiple iterations or operations on it, it'll cause it to be evaluated multiple times -- so in those cases .ToList() makes sense for sure.






    share|improve this answer




























      3














      A couple issues:




      • You need to SelectMany from the MailMessage object to get all attachments in a single collection

      • You're selecting y.Content which is byte but trying to get a list of Attachment

      • LINQ returns IEnumerables, not Lists


      Rewritten, you probably want to do something like:



      List<MailMessage> list = new List<MailMessage>();
      IEnumerable<Attachment> pdfAttachmentsCollection =
      list.SelectMany(message =>
      message.Attachments
      .Where(attachment => attachment.Name.EndsWith(".pdf")));


      This is also a good candidate to write as a query expression:



      IEnumerable<Attachment> pdfAttachmentsCollection = 
      from message in list
      from attachment in message.Attachments
      where attachment.Name.EndsWith(".pdf")
      select attachment;


      Both of these do the same thing, just with different syntax.



      If you want this as a List<Attachment> you can also do .ToList() at the end of either query. As an IEnumerable, it won't be evaluated until you start iterating through it (and depending on how you use it, you might never have the full collection in memory at any time). However, if you do multiple iterations or operations on it, it'll cause it to be evaluated multiple times -- so in those cases .ToList() makes sense for sure.






      share|improve this answer


























        3












        3








        3







        A couple issues:




        • You need to SelectMany from the MailMessage object to get all attachments in a single collection

        • You're selecting y.Content which is byte but trying to get a list of Attachment

        • LINQ returns IEnumerables, not Lists


        Rewritten, you probably want to do something like:



        List<MailMessage> list = new List<MailMessage>();
        IEnumerable<Attachment> pdfAttachmentsCollection =
        list.SelectMany(message =>
        message.Attachments
        .Where(attachment => attachment.Name.EndsWith(".pdf")));


        This is also a good candidate to write as a query expression:



        IEnumerable<Attachment> pdfAttachmentsCollection = 
        from message in list
        from attachment in message.Attachments
        where attachment.Name.EndsWith(".pdf")
        select attachment;


        Both of these do the same thing, just with different syntax.



        If you want this as a List<Attachment> you can also do .ToList() at the end of either query. As an IEnumerable, it won't be evaluated until you start iterating through it (and depending on how you use it, you might never have the full collection in memory at any time). However, if you do multiple iterations or operations on it, it'll cause it to be evaluated multiple times -- so in those cases .ToList() makes sense for sure.






        share|improve this answer













        A couple issues:




        • You need to SelectMany from the MailMessage object to get all attachments in a single collection

        • You're selecting y.Content which is byte but trying to get a list of Attachment

        • LINQ returns IEnumerables, not Lists


        Rewritten, you probably want to do something like:



        List<MailMessage> list = new List<MailMessage>();
        IEnumerable<Attachment> pdfAttachmentsCollection =
        list.SelectMany(message =>
        message.Attachments
        .Where(attachment => attachment.Name.EndsWith(".pdf")));


        This is also a good candidate to write as a query expression:



        IEnumerable<Attachment> pdfAttachmentsCollection = 
        from message in list
        from attachment in message.Attachments
        where attachment.Name.EndsWith(".pdf")
        select attachment;


        Both of these do the same thing, just with different syntax.



        If you want this as a List<Attachment> you can also do .ToList() at the end of either query. As an IEnumerable, it won't be evaluated until you start iterating through it (and depending on how you use it, you might never have the full collection in memory at any time). However, if you do multiple iterations or operations on it, it'll cause it to be evaluated multiple times -- so in those cases .ToList() makes sense for sure.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 4:18









        gregmacgregmac

        18.7k768102




        18.7k768102
































            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%2f53440492%2fc-sharp-linq-select-objects-in-list-looking-inside-another-list-of-objects%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

            How to send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?