How to compare 2 comma seperated string values and update in existing list at same position?
I am having a list of string which contains some value and I want to compare values of 2 positions from list and remove matching items from list.
Code :
var list = new List<string>();
list.Add("Employee1");
list.Add("Account");
list.Add("100.5600,A+ ,John");
list.Add("1.00000,A+ ,John");
list.Add("USA");
Now i want to compare 2nd and 3rd position :
list.Add("100.5600,A+ ,John");
list.Add("1.00000,A+ ,John");
Compare above 2 records and remove matching records like below:
Expected output :
list.Add("100.5600");
list.Add("1.00000");
This is how i am trying to do :
var source = list[2].Split(',').Select(p => p.Trim());
var target = list[3].Split(',').Select(p => p.Trim());
var result = source.Except(target);
But the problem is I am only getting 100.5600 as output.
Is it possible to compare and update non matching records in existing list?
c# linq
|
show 1 more comment
I am having a list of string which contains some value and I want to compare values of 2 positions from list and remove matching items from list.
Code :
var list = new List<string>();
list.Add("Employee1");
list.Add("Account");
list.Add("100.5600,A+ ,John");
list.Add("1.00000,A+ ,John");
list.Add("USA");
Now i want to compare 2nd and 3rd position :
list.Add("100.5600,A+ ,John");
list.Add("1.00000,A+ ,John");
Compare above 2 records and remove matching records like below:
Expected output :
list.Add("100.5600");
list.Add("1.00000");
This is how i am trying to do :
var source = list[2].Split(',').Select(p => p.Trim());
var target = list[3].Split(',').Select(p => p.Trim());
var result = source.Except(target);
But the problem is I am only getting 100.5600 as output.
Is it possible to compare and update non matching records in existing list?
c# linq
for clarification. So you want actually to manipulate the original list and removeA+ ,Johnfrom the entries ?=!
– Mong Zhu
Nov 13 '18 at 10:39
@MongZhu Yeah exactly.I want to remove matching items 2nd and 3rd position by comparing values of those 2 positions
– Mr Bean
Nov 13 '18 at 10:40
Split and trim before you add, problem solved. Actually, I don't think I understand what you are asking. What are your expected results, what are your actual results?
– Jodrell
Nov 13 '18 at 10:41
@Jodrell I have already mention expected output as well as my actual results.
– Mr Bean
Nov 13 '18 at 10:54
can you please define matching ? the amount of spaces afterA+is different in the 2 entiries. So practically onlyJohnis a match. But you want both substrings removed
– Mong Zhu
Nov 13 '18 at 11:39
|
show 1 more comment
I am having a list of string which contains some value and I want to compare values of 2 positions from list and remove matching items from list.
Code :
var list = new List<string>();
list.Add("Employee1");
list.Add("Account");
list.Add("100.5600,A+ ,John");
list.Add("1.00000,A+ ,John");
list.Add("USA");
Now i want to compare 2nd and 3rd position :
list.Add("100.5600,A+ ,John");
list.Add("1.00000,A+ ,John");
Compare above 2 records and remove matching records like below:
Expected output :
list.Add("100.5600");
list.Add("1.00000");
This is how i am trying to do :
var source = list[2].Split(',').Select(p => p.Trim());
var target = list[3].Split(',').Select(p => p.Trim());
var result = source.Except(target);
But the problem is I am only getting 100.5600 as output.
Is it possible to compare and update non matching records in existing list?
c# linq
I am having a list of string which contains some value and I want to compare values of 2 positions from list and remove matching items from list.
Code :
var list = new List<string>();
list.Add("Employee1");
list.Add("Account");
list.Add("100.5600,A+ ,John");
list.Add("1.00000,A+ ,John");
list.Add("USA");
Now i want to compare 2nd and 3rd position :
list.Add("100.5600,A+ ,John");
list.Add("1.00000,A+ ,John");
Compare above 2 records and remove matching records like below:
Expected output :
list.Add("100.5600");
list.Add("1.00000");
This is how i am trying to do :
var source = list[2].Split(',').Select(p => p.Trim());
var target = list[3].Split(',').Select(p => p.Trim());
var result = source.Except(target);
But the problem is I am only getting 100.5600 as output.
Is it possible to compare and update non matching records in existing list?
c# linq
c# linq
asked Nov 13 '18 at 10:17
Mr BeanMr Bean
466217
466217
for clarification. So you want actually to manipulate the original list and removeA+ ,Johnfrom the entries ?=!
– Mong Zhu
Nov 13 '18 at 10:39
@MongZhu Yeah exactly.I want to remove matching items 2nd and 3rd position by comparing values of those 2 positions
– Mr Bean
Nov 13 '18 at 10:40
Split and trim before you add, problem solved. Actually, I don't think I understand what you are asking. What are your expected results, what are your actual results?
– Jodrell
Nov 13 '18 at 10:41
@Jodrell I have already mention expected output as well as my actual results.
– Mr Bean
Nov 13 '18 at 10:54
can you please define matching ? the amount of spaces afterA+is different in the 2 entiries. So practically onlyJohnis a match. But you want both substrings removed
– Mong Zhu
Nov 13 '18 at 11:39
|
show 1 more comment
for clarification. So you want actually to manipulate the original list and removeA+ ,Johnfrom the entries ?=!
– Mong Zhu
Nov 13 '18 at 10:39
@MongZhu Yeah exactly.I want to remove matching items 2nd and 3rd position by comparing values of those 2 positions
– Mr Bean
Nov 13 '18 at 10:40
Split and trim before you add, problem solved. Actually, I don't think I understand what you are asking. What are your expected results, what are your actual results?
– Jodrell
Nov 13 '18 at 10:41
@Jodrell I have already mention expected output as well as my actual results.
– Mr Bean
Nov 13 '18 at 10:54
can you please define matching ? the amount of spaces afterA+is different in the 2 entiries. So practically onlyJohnis a match. But you want both substrings removed
– Mong Zhu
Nov 13 '18 at 11:39
for clarification. So you want actually to manipulate the original list and remove
A+ ,John from the entries ?=!– Mong Zhu
Nov 13 '18 at 10:39
for clarification. So you want actually to manipulate the original list and remove
A+ ,John from the entries ?=!– Mong Zhu
Nov 13 '18 at 10:39
@MongZhu Yeah exactly.I want to remove matching items 2nd and 3rd position by comparing values of those 2 positions
– Mr Bean
Nov 13 '18 at 10:40
@MongZhu Yeah exactly.I want to remove matching items 2nd and 3rd position by comparing values of those 2 positions
– Mr Bean
Nov 13 '18 at 10:40
Split and trim before you add, problem solved. Actually, I don't think I understand what you are asking. What are your expected results, what are your actual results?
– Jodrell
Nov 13 '18 at 10:41
Split and trim before you add, problem solved. Actually, I don't think I understand what you are asking. What are your expected results, what are your actual results?
– Jodrell
Nov 13 '18 at 10:41
@Jodrell I have already mention expected output as well as my actual results.
– Mr Bean
Nov 13 '18 at 10:54
@Jodrell I have already mention expected output as well as my actual results.
– Mr Bean
Nov 13 '18 at 10:54
can you please define matching ? the amount of spaces after
A+ is different in the 2 entiries. So practically only John is a match. But you want both substrings removed– Mong Zhu
Nov 13 '18 at 11:39
can you please define matching ? the amount of spaces after
A+ is different in the 2 entiries. So practically only John is a match. But you want both substrings removed– Mong Zhu
Nov 13 '18 at 11:39
|
show 1 more comment
2 Answers
2
active
oldest
votes
How about this "beauty"
var list = new List<string>();
list.Add("Employee1");
list.Add("Account");
list.Add("100.5600,A+ ,John");
list.Add("1.00000,A+ ,John");
list.Add("USA");
//prepare the list, I decided to make a tuple with the original string in the list and the splitted array
var preparedItems = list.Select(x => (x, x.Split(',')));
//group the prepared list to get matching items for the 2nd and 3rd part of the split, I therefor used .Skip(1) on the previously prepared array
var groupedItems = preparedItems.GroupBy(x => string.Join(",", x.Item2.Skip(1).Select(y => y.Trim())));
//"evaluate" the group by saying if the items in the group is > 1 only use the first part of the prepared array and if it doesnt have more than one entry use the orignal string
var evaluatedItems = groupedItems.SelectMany(x => x.Count() > 1 ? x.Select(y => y.Item2[0]) : x.Select(y => y.Item1));
//replace the orignal list with the new result
list = evaluatedItems.ToList();
Edit - preserve original order:
//extended the prepare routine with a third part the index to Keep track of the ordering of the original list
//so the tuple now consits of 3 parts instead of 2 - ([item], [index], [splittedArray])
var preparedItems = list.Select((x, i) => (x, i, x.Split(',')));
//changed to use Item3 intead of Item2 - since the Array now is on third position
var groupedItems = preparedItems.GroupBy(x => string.Join(",", x.Item3.Skip(1).Select(y => y.Trim())));
//instead of returning the simple string here already, return a tuple with the index (y.Item2) and the correct string
var evaluatedItems = groupedItems.SelectMany(x => x.Count() > 1 ? x.Select(y => (y.Item2, y.Item3[0])) : x.Select(y => (y.Item2, y.Item1)));
//now order by the new tuple x.Item1 and only return x.Item2
var orderedItems = evaluatedItems.OrderBy(x => x.Item1).Select(x => x.Item2);
list = orderedItems.ToList();
//one-liner - isn't that a beauty
list = list.Select((x, i) => (x, i, x.Split(','))).GroupBy(x => string.Join(",", x.Item3.Skip(1).Select(y => y.Trim()))).SelectMany(x => x.Count() > 1 ? x.Select(y => (y.Item2, y.Item3[0])) : x.Select(y => (y.Item2, y.Item1))).OrderBy(x => x.Item1).Select(x => x.Item2).ToList();
But i want to compare only 2nd and 3rd position so it that linq query comparing and manipulating only 2nd and 3rd position?
– Mr Bean
Nov 13 '18 at 11:03
You have the only working solution using the entire original list. But I really hesitate to upvote it, because it is cryptic (unreadable) as hell and has no explanation....
– Mong Zhu
Nov 13 '18 at 11:46
@User Did you try it? I used.Skip(1)to move 1 place Forward, so only 2nd and 3rd Position are used (and 4th, 5th, 6th … xth if there are more).
– Rand Random
Nov 13 '18 at 11:46
1
@MongZhu - now with explanation
– Rand Random
Nov 13 '18 at 11:55
1
@User - yes, in fact this happens - I will think about a solution that can preserve the order - will come back to you
– Rand Random
Nov 13 '18 at 15:31
|
show 4 more comments
You may get it easily by checking if items in one is not contained in the other:
var result = source.Where(x => !target.Contains(x));
To update your old list:
var source = string.Join(",", source.Where(x => !target.Contains(x)));
Thank you so much for answering but I wanted to update existing list based on comparision instead of taking result in another variable.Is it possible?
– Mr Bean
Nov 13 '18 at 10:34
@User, yes, please see my updated answer.
– Ashkan Mobayen Khiabani
Nov 13 '18 at 10:40
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%2f53278697%2fhow-to-compare-2-comma-seperated-string-values-and-update-in-existing-list-at-sa%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
How about this "beauty"
var list = new List<string>();
list.Add("Employee1");
list.Add("Account");
list.Add("100.5600,A+ ,John");
list.Add("1.00000,A+ ,John");
list.Add("USA");
//prepare the list, I decided to make a tuple with the original string in the list and the splitted array
var preparedItems = list.Select(x => (x, x.Split(',')));
//group the prepared list to get matching items for the 2nd and 3rd part of the split, I therefor used .Skip(1) on the previously prepared array
var groupedItems = preparedItems.GroupBy(x => string.Join(",", x.Item2.Skip(1).Select(y => y.Trim())));
//"evaluate" the group by saying if the items in the group is > 1 only use the first part of the prepared array and if it doesnt have more than one entry use the orignal string
var evaluatedItems = groupedItems.SelectMany(x => x.Count() > 1 ? x.Select(y => y.Item2[0]) : x.Select(y => y.Item1));
//replace the orignal list with the new result
list = evaluatedItems.ToList();
Edit - preserve original order:
//extended the prepare routine with a third part the index to Keep track of the ordering of the original list
//so the tuple now consits of 3 parts instead of 2 - ([item], [index], [splittedArray])
var preparedItems = list.Select((x, i) => (x, i, x.Split(',')));
//changed to use Item3 intead of Item2 - since the Array now is on third position
var groupedItems = preparedItems.GroupBy(x => string.Join(",", x.Item3.Skip(1).Select(y => y.Trim())));
//instead of returning the simple string here already, return a tuple with the index (y.Item2) and the correct string
var evaluatedItems = groupedItems.SelectMany(x => x.Count() > 1 ? x.Select(y => (y.Item2, y.Item3[0])) : x.Select(y => (y.Item2, y.Item1)));
//now order by the new tuple x.Item1 and only return x.Item2
var orderedItems = evaluatedItems.OrderBy(x => x.Item1).Select(x => x.Item2);
list = orderedItems.ToList();
//one-liner - isn't that a beauty
list = list.Select((x, i) => (x, i, x.Split(','))).GroupBy(x => string.Join(",", x.Item3.Skip(1).Select(y => y.Trim()))).SelectMany(x => x.Count() > 1 ? x.Select(y => (y.Item2, y.Item3[0])) : x.Select(y => (y.Item2, y.Item1))).OrderBy(x => x.Item1).Select(x => x.Item2).ToList();
But i want to compare only 2nd and 3rd position so it that linq query comparing and manipulating only 2nd and 3rd position?
– Mr Bean
Nov 13 '18 at 11:03
You have the only working solution using the entire original list. But I really hesitate to upvote it, because it is cryptic (unreadable) as hell and has no explanation....
– Mong Zhu
Nov 13 '18 at 11:46
@User Did you try it? I used.Skip(1)to move 1 place Forward, so only 2nd and 3rd Position are used (and 4th, 5th, 6th … xth if there are more).
– Rand Random
Nov 13 '18 at 11:46
1
@MongZhu - now with explanation
– Rand Random
Nov 13 '18 at 11:55
1
@User - yes, in fact this happens - I will think about a solution that can preserve the order - will come back to you
– Rand Random
Nov 13 '18 at 15:31
|
show 4 more comments
How about this "beauty"
var list = new List<string>();
list.Add("Employee1");
list.Add("Account");
list.Add("100.5600,A+ ,John");
list.Add("1.00000,A+ ,John");
list.Add("USA");
//prepare the list, I decided to make a tuple with the original string in the list and the splitted array
var preparedItems = list.Select(x => (x, x.Split(',')));
//group the prepared list to get matching items for the 2nd and 3rd part of the split, I therefor used .Skip(1) on the previously prepared array
var groupedItems = preparedItems.GroupBy(x => string.Join(",", x.Item2.Skip(1).Select(y => y.Trim())));
//"evaluate" the group by saying if the items in the group is > 1 only use the first part of the prepared array and if it doesnt have more than one entry use the orignal string
var evaluatedItems = groupedItems.SelectMany(x => x.Count() > 1 ? x.Select(y => y.Item2[0]) : x.Select(y => y.Item1));
//replace the orignal list with the new result
list = evaluatedItems.ToList();
Edit - preserve original order:
//extended the prepare routine with a third part the index to Keep track of the ordering of the original list
//so the tuple now consits of 3 parts instead of 2 - ([item], [index], [splittedArray])
var preparedItems = list.Select((x, i) => (x, i, x.Split(',')));
//changed to use Item3 intead of Item2 - since the Array now is on third position
var groupedItems = preparedItems.GroupBy(x => string.Join(",", x.Item3.Skip(1).Select(y => y.Trim())));
//instead of returning the simple string here already, return a tuple with the index (y.Item2) and the correct string
var evaluatedItems = groupedItems.SelectMany(x => x.Count() > 1 ? x.Select(y => (y.Item2, y.Item3[0])) : x.Select(y => (y.Item2, y.Item1)));
//now order by the new tuple x.Item1 and only return x.Item2
var orderedItems = evaluatedItems.OrderBy(x => x.Item1).Select(x => x.Item2);
list = orderedItems.ToList();
//one-liner - isn't that a beauty
list = list.Select((x, i) => (x, i, x.Split(','))).GroupBy(x => string.Join(",", x.Item3.Skip(1).Select(y => y.Trim()))).SelectMany(x => x.Count() > 1 ? x.Select(y => (y.Item2, y.Item3[0])) : x.Select(y => (y.Item2, y.Item1))).OrderBy(x => x.Item1).Select(x => x.Item2).ToList();
But i want to compare only 2nd and 3rd position so it that linq query comparing and manipulating only 2nd and 3rd position?
– Mr Bean
Nov 13 '18 at 11:03
You have the only working solution using the entire original list. But I really hesitate to upvote it, because it is cryptic (unreadable) as hell and has no explanation....
– Mong Zhu
Nov 13 '18 at 11:46
@User Did you try it? I used.Skip(1)to move 1 place Forward, so only 2nd and 3rd Position are used (and 4th, 5th, 6th … xth if there are more).
– Rand Random
Nov 13 '18 at 11:46
1
@MongZhu - now with explanation
– Rand Random
Nov 13 '18 at 11:55
1
@User - yes, in fact this happens - I will think about a solution that can preserve the order - will come back to you
– Rand Random
Nov 13 '18 at 15:31
|
show 4 more comments
How about this "beauty"
var list = new List<string>();
list.Add("Employee1");
list.Add("Account");
list.Add("100.5600,A+ ,John");
list.Add("1.00000,A+ ,John");
list.Add("USA");
//prepare the list, I decided to make a tuple with the original string in the list and the splitted array
var preparedItems = list.Select(x => (x, x.Split(',')));
//group the prepared list to get matching items for the 2nd and 3rd part of the split, I therefor used .Skip(1) on the previously prepared array
var groupedItems = preparedItems.GroupBy(x => string.Join(",", x.Item2.Skip(1).Select(y => y.Trim())));
//"evaluate" the group by saying if the items in the group is > 1 only use the first part of the prepared array and if it doesnt have more than one entry use the orignal string
var evaluatedItems = groupedItems.SelectMany(x => x.Count() > 1 ? x.Select(y => y.Item2[0]) : x.Select(y => y.Item1));
//replace the orignal list with the new result
list = evaluatedItems.ToList();
Edit - preserve original order:
//extended the prepare routine with a third part the index to Keep track of the ordering of the original list
//so the tuple now consits of 3 parts instead of 2 - ([item], [index], [splittedArray])
var preparedItems = list.Select((x, i) => (x, i, x.Split(',')));
//changed to use Item3 intead of Item2 - since the Array now is on third position
var groupedItems = preparedItems.GroupBy(x => string.Join(",", x.Item3.Skip(1).Select(y => y.Trim())));
//instead of returning the simple string here already, return a tuple with the index (y.Item2) and the correct string
var evaluatedItems = groupedItems.SelectMany(x => x.Count() > 1 ? x.Select(y => (y.Item2, y.Item3[0])) : x.Select(y => (y.Item2, y.Item1)));
//now order by the new tuple x.Item1 and only return x.Item2
var orderedItems = evaluatedItems.OrderBy(x => x.Item1).Select(x => x.Item2);
list = orderedItems.ToList();
//one-liner - isn't that a beauty
list = list.Select((x, i) => (x, i, x.Split(','))).GroupBy(x => string.Join(",", x.Item3.Skip(1).Select(y => y.Trim()))).SelectMany(x => x.Count() > 1 ? x.Select(y => (y.Item2, y.Item3[0])) : x.Select(y => (y.Item2, y.Item1))).OrderBy(x => x.Item1).Select(x => x.Item2).ToList();
How about this "beauty"
var list = new List<string>();
list.Add("Employee1");
list.Add("Account");
list.Add("100.5600,A+ ,John");
list.Add("1.00000,A+ ,John");
list.Add("USA");
//prepare the list, I decided to make a tuple with the original string in the list and the splitted array
var preparedItems = list.Select(x => (x, x.Split(',')));
//group the prepared list to get matching items for the 2nd and 3rd part of the split, I therefor used .Skip(1) on the previously prepared array
var groupedItems = preparedItems.GroupBy(x => string.Join(",", x.Item2.Skip(1).Select(y => y.Trim())));
//"evaluate" the group by saying if the items in the group is > 1 only use the first part of the prepared array and if it doesnt have more than one entry use the orignal string
var evaluatedItems = groupedItems.SelectMany(x => x.Count() > 1 ? x.Select(y => y.Item2[0]) : x.Select(y => y.Item1));
//replace the orignal list with the new result
list = evaluatedItems.ToList();
Edit - preserve original order:
//extended the prepare routine with a third part the index to Keep track of the ordering of the original list
//so the tuple now consits of 3 parts instead of 2 - ([item], [index], [splittedArray])
var preparedItems = list.Select((x, i) => (x, i, x.Split(',')));
//changed to use Item3 intead of Item2 - since the Array now is on third position
var groupedItems = preparedItems.GroupBy(x => string.Join(",", x.Item3.Skip(1).Select(y => y.Trim())));
//instead of returning the simple string here already, return a tuple with the index (y.Item2) and the correct string
var evaluatedItems = groupedItems.SelectMany(x => x.Count() > 1 ? x.Select(y => (y.Item2, y.Item3[0])) : x.Select(y => (y.Item2, y.Item1)));
//now order by the new tuple x.Item1 and only return x.Item2
var orderedItems = evaluatedItems.OrderBy(x => x.Item1).Select(x => x.Item2);
list = orderedItems.ToList();
//one-liner - isn't that a beauty
list = list.Select((x, i) => (x, i, x.Split(','))).GroupBy(x => string.Join(",", x.Item3.Skip(1).Select(y => y.Trim()))).SelectMany(x => x.Count() > 1 ? x.Select(y => (y.Item2, y.Item3[0])) : x.Select(y => (y.Item2, y.Item1))).OrderBy(x => x.Item1).Select(x => x.Item2).ToList();
edited Nov 13 '18 at 16:19
answered Nov 13 '18 at 10:56
Rand RandomRand Random
2,95073163
2,95073163
But i want to compare only 2nd and 3rd position so it that linq query comparing and manipulating only 2nd and 3rd position?
– Mr Bean
Nov 13 '18 at 11:03
You have the only working solution using the entire original list. But I really hesitate to upvote it, because it is cryptic (unreadable) as hell and has no explanation....
– Mong Zhu
Nov 13 '18 at 11:46
@User Did you try it? I used.Skip(1)to move 1 place Forward, so only 2nd and 3rd Position are used (and 4th, 5th, 6th … xth if there are more).
– Rand Random
Nov 13 '18 at 11:46
1
@MongZhu - now with explanation
– Rand Random
Nov 13 '18 at 11:55
1
@User - yes, in fact this happens - I will think about a solution that can preserve the order - will come back to you
– Rand Random
Nov 13 '18 at 15:31
|
show 4 more comments
But i want to compare only 2nd and 3rd position so it that linq query comparing and manipulating only 2nd and 3rd position?
– Mr Bean
Nov 13 '18 at 11:03
You have the only working solution using the entire original list. But I really hesitate to upvote it, because it is cryptic (unreadable) as hell and has no explanation....
– Mong Zhu
Nov 13 '18 at 11:46
@User Did you try it? I used.Skip(1)to move 1 place Forward, so only 2nd and 3rd Position are used (and 4th, 5th, 6th … xth if there are more).
– Rand Random
Nov 13 '18 at 11:46
1
@MongZhu - now with explanation
– Rand Random
Nov 13 '18 at 11:55
1
@User - yes, in fact this happens - I will think about a solution that can preserve the order - will come back to you
– Rand Random
Nov 13 '18 at 15:31
But i want to compare only 2nd and 3rd position so it that linq query comparing and manipulating only 2nd and 3rd position?
– Mr Bean
Nov 13 '18 at 11:03
But i want to compare only 2nd and 3rd position so it that linq query comparing and manipulating only 2nd and 3rd position?
– Mr Bean
Nov 13 '18 at 11:03
You have the only working solution using the entire original list. But I really hesitate to upvote it, because it is cryptic (unreadable) as hell and has no explanation....
– Mong Zhu
Nov 13 '18 at 11:46
You have the only working solution using the entire original list. But I really hesitate to upvote it, because it is cryptic (unreadable) as hell and has no explanation....
– Mong Zhu
Nov 13 '18 at 11:46
@User Did you try it? I used
.Skip(1) to move 1 place Forward, so only 2nd and 3rd Position are used (and 4th, 5th, 6th … xth if there are more).– Rand Random
Nov 13 '18 at 11:46
@User Did you try it? I used
.Skip(1) to move 1 place Forward, so only 2nd and 3rd Position are used (and 4th, 5th, 6th … xth if there are more).– Rand Random
Nov 13 '18 at 11:46
1
1
@MongZhu - now with explanation
– Rand Random
Nov 13 '18 at 11:55
@MongZhu - now with explanation
– Rand Random
Nov 13 '18 at 11:55
1
1
@User - yes, in fact this happens - I will think about a solution that can preserve the order - will come back to you
– Rand Random
Nov 13 '18 at 15:31
@User - yes, in fact this happens - I will think about a solution that can preserve the order - will come back to you
– Rand Random
Nov 13 '18 at 15:31
|
show 4 more comments
You may get it easily by checking if items in one is not contained in the other:
var result = source.Where(x => !target.Contains(x));
To update your old list:
var source = string.Join(",", source.Where(x => !target.Contains(x)));
Thank you so much for answering but I wanted to update existing list based on comparision instead of taking result in another variable.Is it possible?
– Mr Bean
Nov 13 '18 at 10:34
@User, yes, please see my updated answer.
– Ashkan Mobayen Khiabani
Nov 13 '18 at 10:40
add a comment |
You may get it easily by checking if items in one is not contained in the other:
var result = source.Where(x => !target.Contains(x));
To update your old list:
var source = string.Join(",", source.Where(x => !target.Contains(x)));
Thank you so much for answering but I wanted to update existing list based on comparision instead of taking result in another variable.Is it possible?
– Mr Bean
Nov 13 '18 at 10:34
@User, yes, please see my updated answer.
– Ashkan Mobayen Khiabani
Nov 13 '18 at 10:40
add a comment |
You may get it easily by checking if items in one is not contained in the other:
var result = source.Where(x => !target.Contains(x));
To update your old list:
var source = string.Join(",", source.Where(x => !target.Contains(x)));
You may get it easily by checking if items in one is not contained in the other:
var result = source.Where(x => !target.Contains(x));
To update your old list:
var source = string.Join(",", source.Where(x => !target.Contains(x)));
edited Nov 13 '18 at 10:39
answered Nov 13 '18 at 10:25
Ashkan Mobayen KhiabaniAshkan Mobayen Khiabani
20.3k1565114
20.3k1565114
Thank you so much for answering but I wanted to update existing list based on comparision instead of taking result in another variable.Is it possible?
– Mr Bean
Nov 13 '18 at 10:34
@User, yes, please see my updated answer.
– Ashkan Mobayen Khiabani
Nov 13 '18 at 10:40
add a comment |
Thank you so much for answering but I wanted to update existing list based on comparision instead of taking result in another variable.Is it possible?
– Mr Bean
Nov 13 '18 at 10:34
@User, yes, please see my updated answer.
– Ashkan Mobayen Khiabani
Nov 13 '18 at 10:40
Thank you so much for answering but I wanted to update existing list based on comparision instead of taking result in another variable.Is it possible?
– Mr Bean
Nov 13 '18 at 10:34
Thank you so much for answering but I wanted to update existing list based on comparision instead of taking result in another variable.Is it possible?
– Mr Bean
Nov 13 '18 at 10:34
@User, yes, please see my updated answer.
– Ashkan Mobayen Khiabani
Nov 13 '18 at 10:40
@User, yes, please see my updated answer.
– Ashkan Mobayen Khiabani
Nov 13 '18 at 10:40
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%2f53278697%2fhow-to-compare-2-comma-seperated-string-values-and-update-in-existing-list-at-sa%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
for clarification. So you want actually to manipulate the original list and remove
A+ ,Johnfrom the entries ?=!– Mong Zhu
Nov 13 '18 at 10:39
@MongZhu Yeah exactly.I want to remove matching items 2nd and 3rd position by comparing values of those 2 positions
– Mr Bean
Nov 13 '18 at 10:40
Split and trim before you add, problem solved. Actually, I don't think I understand what you are asking. What are your expected results, what are your actual results?
– Jodrell
Nov 13 '18 at 10:41
@Jodrell I have already mention expected output as well as my actual results.
– Mr Bean
Nov 13 '18 at 10:54
can you please define matching ? the amount of spaces after
A+is different in the 2 entiries. So practically onlyJohnis a match. But you want both substrings removed– Mong Zhu
Nov 13 '18 at 11:39