c#将字符串转换为IEnumerable< T> [英] c# Cast string as IEnumerable<T>

查看:293
本文介绍了c#将字符串转换为IEnumerable< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个函数:

  public static IEnumerable< T> UniqueInOrder< T>(IEnumerable< T>可迭代)
{
return iterable.Distinct();
}

这适用于任何IEnumerable,除了字符串,我必须用此测试调用:

  Assert.AreEqual(ABCDAB,UniqueInOrder(AAAABBBCCDAABBB)); 

断言失败:

  Expected is< System.String> ;, actual is< System.Linq.Enumerable +< DistinctIterator> c__Iterator10`1 [System.Char]> 
值在索引[4]处不同

我也尝试了类似的方法:

  public static IEnumerable< T> UniqueInOrder< T>(IEnumerable< T>可迭代)
{
returnabc;
}

但是我有一个编译器错误:

 不能将类型'string'隐式转换为'System.Collections.Generic.IEnumerable< T>'

我可以用一个字符串调用函数,但是我不能返回一个字符串?该类型仍然是相同的IEnumerable< T>



有什么想法?
Thank you!


编辑:
Distinct()错了,我改成了这个:

  public static IEnumerable< T> UniqueInOrder< T>(IEnumerable< T>可迭代)
{
List< T> result = new List< T>();
foreach(var c in iterable.ToList())
if(result.LastOrDefault()== null ||!result.LastOrDefault()。Equals(c))
result.Add (C);
返回结果;
}

现在所有的测试都通过了!感谢!

解决方案

实际上,一个字符串是一个字符集合,因此当您使用 UniqueInOrder(AAAABBBCCDAABBB )你调用 UniqueInOrder< char> ,而不是 UniqueInOrder< string> 。因此,该方法的返回值也是 IEnumerble ,而不仅仅是字符串。 应该比较方法的返回值与一个字符集合,例如:

  CollectionAssert.AreEqual(new [] {'A' ,'B','C','D','A''B','Ä'},UniqueInOrder(...)); 

或更简单:

  CollectionAssert.AreEqual(expected.ToCharArray(),UniqueInOrder(...)); 

但即使如此,您的测试也会失败,因为 Distinct 会过滤掉所有重复项目。



当您想检查两个序列是否完全相同时,您可以使用 SequenceEqual 代替:

  var equal = firstColl.SequenceEqual(second); 

编辑:很明显,你试图删除序列中的所有双倍字符。您可以使用这个:

  public static IEnumerable< T> UniqueInOrder< T>(IEnumerable< T> iterable)其中T:IEquatable< T> 
{
T previous = default(T);
foreach(迭代中的var t)
{
if(!t.Equals(previous))
yield return t;
previous = t;


$ / code $ / pre

现在你可以调用它并与你的期望值进行比较输出,例如:

  var actual = new String(UniqueInOrder(AABBCCDDAABB)。ToArray()); 
var期望ABCDAB;
Assert.AreEqual(预计,实际);


I have this function:

public static IEnumerable<T> UniqueInOrder<T>(IEnumerable<T> iterable)  
{  
    return iterable.Distinct();  
}

This works with any IEnumerable except with a string, I must test it with this call:

Assert.AreEqual("ABCDAB", UniqueInOrder("AAAABBBCCDAABBB"));

The assert fail:

Expected is <System.String>, actual is <System.Linq.Enumerable+<DistinctIterator>c__Iterator10`1[System.Char]>
  Values differ at index [4]

I also tried something like:

public static IEnumerable<T> UniqueInOrder<T>(IEnumerable<T> iterable)
{
    return "abc";
}

But I have a compiler error:

Cannot implicitly convert type 'string' to 'System.Collections.Generic.IEnumerable<T>'

How is possible that I can call the function with a string but I can not return a string? The type is still the same IEnumerable< T >

Any idea? Thank you!

EDITED: Distinct() was wrong, I changed to this:

public static IEnumerable<T> UniqueInOrder<T>(IEnumerable<T> iterable)
{
    List<T> result = new List<T>();
    foreach (var c in iterable.ToList())            
        if (result.LastOrDefault() == null || !result.LastOrDefault().Equals(c))
            result.Add(c);              
    return result;
}

Now all the tests are passing! Thanks!

解决方案

Actually a string is a collection of characters, thus when you use UniqueInOrder("AAAABBBCCDAABBB") you actuall call UniqueInOrder<char>, instead of UniqueInOrder<string>. Thus the return-value of the method will also be IEnumerble<char>, not just string.

So you should compare the methods return-value with a collection of characters, e.g.:

CollectionAssert.AreEqual(new[] { 'A', 'B', 'C', 'D', 'A' 'B', 'Ä' }, UniqueInOrder(...));

or easier:

CollectionAssert.AreEqual(expected.ToCharArray(), UniqueInOrder(...));

But even then your test will fail as Distinct will filter out all duplicates.

When you want to check if two sequences are completely identical you may use SequenceEqual instead:

var equal = firstColl.SequenceEqual(second);

EDIT: Obviously you´re trying to remove all just the doubled characters in a sequence. You may use this:

public static IEnumerable<T> UniqueInOrder<T>(IEnumerable<T> iterable) where T : IEquatable<T>
{
    T previous = default(T);
    foreach (var t in iterable)
    {
        if (!t.Equals(previous))
            yield return t;
        previous = t;
    }
}

Now you can call it and compare it with your expected output, e.g.:

var actual = new String(UniqueInOrder("AABBCCDDAABB").ToArray());
var expected "ABCDAB";
Assert.AreEqual(expected, actual);

这篇关于c#将字符串转换为IEnumerable&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆