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;
}
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
add a comment |
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
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 withlist.SelectMany(x => x.Attachments). The rest is standardWherestuff 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
add a comment |
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
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
c# linq
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 withlist.SelectMany(x => x.Attachments). The rest is standardWherestuff 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
add a comment |
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 withlist.SelectMany(x => x.Attachments). The rest is standardWherestuff 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
add a comment |
1 Answer
1
active
oldest
votes
A couple issues:
- You need to
SelectManyfrom theMailMessageobject to get all attachments in a single collection - You're selecting
y.Contentwhich isbytebut trying to get a list ofAttachment
- 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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
A couple issues:
- You need to
SelectManyfrom theMailMessageobject to get all attachments in a single collection - You're selecting
y.Contentwhich isbytebut trying to get a list ofAttachment
- 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.
add a comment |
A couple issues:
- You need to
SelectManyfrom theMailMessageobject to get all attachments in a single collection - You're selecting
y.Contentwhich isbytebut trying to get a list ofAttachment
- 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.
add a comment |
A couple issues:
- You need to
SelectManyfrom theMailMessageobject to get all attachments in a single collection - You're selecting
y.Contentwhich isbytebut trying to get a list ofAttachment
- 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.
A couple issues:
- You need to
SelectManyfrom theMailMessageobject to get all attachments in a single collection - You're selecting
y.Contentwhich isbytebut trying to get a list ofAttachment
- 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.
answered Nov 23 '18 at 4:18
gregmacgregmac
18.7k768102
18.7k768102
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 standardWherestuff 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