C# Linq - Cannot implicitly convert IEnumerable to List
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
add a comment |
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
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
add a comment |
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
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
c# linq
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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();
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
add a comment |
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
.
add a comment |
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.
7
why do you give the same answer eight hours after someone else did it?
– Oliver
May 18 '10 at 8:08
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%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
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();
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
add a comment |
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();
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
add a comment |
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();
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();
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
add a comment |
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
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
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
.
edited Sep 28 '15 at 11:49
answered May 17 '10 at 23:30
juharrjuharr
25.4k33676
25.4k33676
add a comment |
add a comment |
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.
7
why do you give the same answer eight hours after someone else did it?
– Oliver
May 18 '10 at 8:08
add a comment |
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.
7
why do you give the same answer eight hours after someone else did it?
– Oliver
May 18 '10 at 8:08
add a comment |
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.
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.
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
add a comment |
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
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%2f2853647%2fc-sharp-linq-cannot-implicitly-convert-ienumerablestring-to-liststring%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
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