[英]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>>'
toSystem.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.