C# 维护以分号分隔的数字序列,并执行添加、删除操作
假设我有一系列的否定,比如:-C# 维护以分号分隔的数字序列,并执行添加、删除操作,c#,logic,C#,Logic,假设我有一系列的否定,比如:- 1;2;3;4;5;6;7;8;9 现在,我想删除一些“不”的说法(3,6,7),因此序列如下所示:- 1;2;4;5;8;9 现在,如果我再次添加删除的no,比如说(3,6),那么这将给出如下顺序:- 1;2;3;4;5;6;8;9 // 7 is not here 将string转换为IEnumerable并使用Linq;最后,借助于Join,将序列转换回string: string source = "1;2;3;4;5;6;7;8;9";
1;2;3;4;5;6;7;8;9
现在,我想删除一些“不”的说法(3,6,7)
,因此序列如下所示:-
1;2;4;5;8;9
现在,如果我再次添加删除的no,比如说(3,6)
,那么这将给出如下顺序:-
1;2;3;4;5;6;8;9 // 7 is not here
将
string
转换为IEnumerable
并使用Linq;最后,借助于Join
,将序列转换回string
:
string source = "1;2;3;4;5;6;7;8;9";
int[] exclude = new int[] {3, 6, 7};
int[] include = new int[] {3, 6};
var seq = source
.Split(';')
.Select(item => int.Parse(item)) // now we have IEnumerable<int>
.Where(item => !exclude.Contains(item)) // removing "exclude"
.Concat(include) // appending "include"
.OrderBy(item => item);
string result = string.Join(";", seq); // back to string
我建议使用GroupBy
而不是Where
:
var seq = source
.Split(';')
.Select(item => int.Parse(item)) // now we have IEnumerable<int>
.GroupBy(item => item) // removing "exclude"
.SelectMany(chunk => chunk
.Skip(exclude.Count(item => item == chunk.Key)))
.Concat(include) // appending "include"
.OrderBy(item => item);
string result = string.Join(";", seq);
var seq=来源
.Split(“;”)
.Select(item=>int.Parse(item))//现在我们有了IEnumerable
.GroupBy(item=>item)//删除“排除”
.SelectMany(chunk=>chunk
.Skip(exclude.Count(item=>item==chunk.Key)))
.Concat(include)//追加“include”
.OrderBy(项目=>项目);
字符串结果=string.Join(“;”,seq);
将字符串
转换为IEnumerable
并使用Linq;最后,借助于Join
,将序列转换回string
:
string source = "1;2;3;4;5;6;7;8;9";
int[] exclude = new int[] {3, 6, 7};
int[] include = new int[] {3, 6};
var seq = source
.Split(';')
.Select(item => int.Parse(item)) // now we have IEnumerable<int>
.Where(item => !exclude.Contains(item)) // removing "exclude"
.Concat(include) // appending "include"
.OrderBy(item => item);
string result = string.Join(";", seq); // back to string
我建议使用GroupBy
而不是Where
:
var seq = source
.Split(';')
.Select(item => int.Parse(item)) // now we have IEnumerable<int>
.GroupBy(item => item) // removing "exclude"
.SelectMany(chunk => chunk
.Skip(exclude.Count(item => item == chunk.Key)))
.Concat(include) // appending "include"
.OrderBy(item => item);
string result = string.Join(";", seq);
var seq=来源
.Split(“;”)
.Select(item=>int.Parse(item))//现在我们有了IEnumerable
.GroupBy(item=>item)//删除“排除”
.SelectMany(chunk=>chunk
.Skip(exclude.Count(item=>item==chunk.Key)))
.Concat(include)//追加“include”
.OrderBy(项目=>项目);
字符串结果=string.Join(“;”,seq);
请将您的数据显示为有效的C#代码,以便我们了解我们正在处理的内容。现在还不清楚这是一个字符串还是一个整数序列。为什么要使用字符串而不是列表
来处理这样的事情呢?如果列表
,则排序集
会更好。到目前为止,您尝试过什么吗?显示您尝试过的内容以及您遇到的具体问题。请将您的数据显示为有效的C#代码,以便我们了解我们正在处理的问题。现在还不清楚这是一个字符串还是一个整数序列。为什么要使用字符串而不是列表
来处理这样的事情呢?如果列表
,则排序集
会更好。到目前为止,您尝试过什么吗?显示你所尝试的,特别是你在哪里遇到问题。也许考虑做<代码>。(排除)。我不确定我们是否在使用集合(这就是为什么我使用IEnumerable
,Where
和Concat
)@DmitryBychenko如果你使用HashSet
,你不能直接使用SortedSet
?@DmitryBychenko-很好。您是否认为在使用.Where(item=>!exclude.Contains(item))
时也存在类似的问题?exclude
中的单个项将删除源中的多个项。@Enigmativity:是,在当前实现中,将删除所有3、6、7
。如果我们有<代码> 3, 3, 3,4代码< 3, 3 >代码>(只有<代码> 2 < /代码> <代码> 4 < /代码>),我建议<代码> GROPPEB/<代码>:<代码> GROMPBY(项目= >项)。SelectMany(Cux= > CUng.Skip(删除.Cub(Cuth.Co())))< >代码>也许考虑做<代码>。<代码> >在中间。“神秘性:<代码>,除了和IEnumerable
,Where
和Concat
)@DmitryBychenko如果你使用HashSet
,你不能直接使用SortedSet
?@DmitryBychenko-很好。您是否认为在使用.Where(item=>!exclude.Contains(item))
时也存在类似的问题?exclude
中的单个项将删除源中的多个项。@Enigmativity:是,在当前实现中,将删除所有3、6、7
。如果我们有3,3,3,4
并且想要删除3,3
(只有2
出现4
),我建议GroupBy
:GroupBy(item=>item)。选择many(chunk=>chunk.Skip(remove.Count(chunk.Count()))