Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 维护以分号分隔的数字序列,并执行添加、删除操作_C#_Logic - Fatal编程技术网

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()))