简体   繁体   English

无法从&#39;System.Collections.Generic.IEnumerable转换为System.Collections.Generic.IEnumerable <string>

[英]Cannot convert from 'System.Collections.Generic.IEnumerable to System.Collections.Generic.IEnumerable<string>

I have a spec flow test with the following test data 我有一个规格测试,包含以下测试数据

    | Field   | Value  |
    | Message | test   |
    | Message2| test2  |

I have a class that gets data from spec flow table 我有一个从规格流表获取数据的类

  public List<String> GetInputMessage(Table table)
    {
        var elements = new List<string>();
        var data = table.CreateSet<SpecFlowData>().ToList();
        elements.AddRange(from item in data
                          let field = item.Field
                          let value = item.Value
                          select new List<string>(new string[] { field ,value}));
        return elements;
    }

but I am getting error: 但我得到错误:

cannot convert from 'System.Collections.Generic.IEnumerable <System.Collections.Generic.List<string>>' to System.Collections.Generic.IEnumerable<string> 无法从'System.Collections.Generic.IEnumerable <System.Collections.Generic.List<string>>'System.Collections.Generic.IEnumerable<string>

What is the other way of getting the field and value from the table? 从表中获取字段和值的另一种方法是什么? thank you 谢谢

Your field name and value pairs appear to be a good match for a dictionary collection instead of list collection. 您的字段名称和值对似乎是字典集合而不是列表集合的良好匹配。

public Dictionary<string, string> GetInputMessage(Table table)
{
    var elements = new Dictionary<string, string>();
    foreach(var item in table.CreateSet<SpecFlowData>())
    {
       elements.Add(item.Field, item.Value);
    }
    return elements;
}

AddRange method expecting an IEnumerable<string> but you are trying to pass it IEnumerable<List<string>> . AddRange方法需要IEnumerable<string>但是您正在尝试将其传递给IEnumerable<List<string>> Try this: 尝试这个:

elements.AddRange(data.SelectMany(x => new [] { x.Field, x.Value}));

Or if you want to concatenate the Field and Value into single row: 或者,如果您想将“ Field和“ Value串联成一行:

elements.AddRange(data.Select(x => string.Join(",", x.Field,x.Value));

While the accepted answer shows how to do it with a dictionary, if you have multiple of the same Field you won't be able to use it. 虽然接受的答案显示了如何使用字典,但是如果您在同一Field有多个,则将无法使用它。 In this situation I would just make a class representing each row, it is not much code and very similar to your original answer (I also cleaned it up some removing some of the extra bits you did not need to have). 在这种情况下,我将只创建一个代表每一行的类,它的代码并不多,并且与您的原始答案非常相似(我也对其进行了清理,删除了一些不需要的多余位)。

//Add this somewhere in your project.
class MyDataRow
{
    public string Field {get; set;}
    public string Value {get; set;}
}

//back to your orginal function.
public List<MyDataRow> GetInputMessage(Table table)
{

    var data = table.CreateSet<SpecFlowData>(); //the ToList() that was here is not needed, just use the IEnumerable<SpecFlowData> or IQueryable<SpecFlowData> that CreateSet<SpecFlowData>() returns.
    var elements = (from item in data
                    //The two let clauses that where here are unessesary too.
                    select new MyDataRow {Field = item.Field, Value = item.Value}
                   ).ToList();
    return elements;
}

Check the declare types and the result after the linq code.. 在linq代码后检查声明类型和结果。

To solve you can do this: 要解决您可以执行以下操作:

public List<String> GetInputMessage(Table table)
{
    var elements = new List<List<string>>();
    var data = table.CreateSet<SpecFlowData>().ToList();
    elements.AddRange(from item in data
                      let field = item.Field
                      let value = item.Value
                      select new List<string>(new string[] { field ,value}));
    return elements.SelectMany(a => a).ToList();
}

But there are other ways to solve your problem... 但是还有其他方法可以解决您的问题...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TreeView:无法从&#39;System.Collections.Generic.IEnumerable转换 <string> &#39;到&#39;System.Collections.Generic.IEnumerable - TreeView: Cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'System.Collections.Generic.IEnumerable 无法从“ System.Collections.Generic.IEnumerable”转换为System.Collections.Generic.IEnumerable <string> - cannot convert from 'System.Collections.Generic.IEnumerable' to System.Collections.Generic.IEnumerable<string> 无法从&#39;System.Collections.Generic.IEnumerable转换 <char> &#39;到&#39;字符串&#39; - cannot convert from 'System.Collections.Generic.IEnumerable<char>' to 'string' 无法从&#39;System.Collections.Generic.IEnumerable转换 <int> &#39;到&#39;System.Collections.Generic.IEnumerable <int?> “ - Cannot convert from 'System.Collections.Generic.IEnumerable<int>' to 'System.Collections.Generic.IEnumerable<int?>' 无法从“System.Collections.Generic.IEnumerable”转换<string> &#39; 到 &#39;T&#39; - cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'T' 从&#39;string&#39;转换为&#39;System.Collections.Generic.IEnumerable <string> - Convert from 'string' to 'System.Collections.Generic.IEnumerable<string> 无法从&#39;System.Collections.Generic.IEnumerable转换 <System.Collection.Generic.List<char> &gt;字符串[] - Cannot convert from 'System.Collections.Generic.IEnumerable<System.Collection.Generic.List<char>> to string[] 无法隐式转换类型&#39;System.Collections.Generic.IEnumerable <string> &#39;到&#39;System.Collections.Generic.ICollection <x> “ - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'System.Collections.Generic.ICollection<x>' 无法从 System.Collections.Generic.List 转换为 System.Collections.Generic.IEnumerable - cannot convert from System.Collections.Generic.List to System.Collections.Generic.IEnumerable 转换&#39;System.Collections.Generic.IEnumerable <string> &#39;至&#39;string [] - Convert 'System.Collections.Generic.IEnumerable<string>' to 'string[]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM