C# 无法强制转换类型为“WhereSelectListIterator”2的对象
下面是我的代码。try中的行抛出以下错误 无法强制转换类型为“WhereSelectListIterator2[System.Tuple3[System.String,System.String,System.String],Syste>m.Boolean]的对象” 输入'System.Tuple'3[System.String,System.String,System.String]' 为什么我不能用下面的方法来施法?我该怎么做C# 无法强制转换类型为“WhereSelectListIterator”2的对象,c#,C#,下面是我的代码。try中的行抛出以下错误 无法强制转换类型为“WhereSelectListIterator2[System.Tuple3[System.String,System.String,System.String],Syste>m.Boolean]的对象” 输入'System.Tuple'3[System.String,System.String,System.String]' 为什么我不能用下面的方法来施法?我该怎么做 List<Tuple<string,
List<Tuple<string, string, string>> customers = new List<Tuple<string, string, string>>();
customers.Add(new Tuple<string,string,string>("Saroj", "India", "Mumbai"));
customers.Add(new Tuple<string, string, string>("Robin", "US", "New York"));
List<string> filterNames = new List<string>();
filterNames.Add("Saroj");
foreach (string name in filterNames)
{
var customer = customers.Select(s => s.Item1 == name);
try
{
Tuple<string, string, string> item = (Tuple<string, string, string>)customer;
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
你必须把它放在哪里,你就会得到你的输出。不需要施放
foreach (string name in filterNames)
{
var customer = customers.Where(s => s.Item1 == name);
}
customers.Selects=>s.Item1==name返回对象的IEnumerable,而不是单个对象。根据业务逻辑使用FirstOrDefault、First、SingleOrDefault或Single从ObjectsOps的IEnumerable获取对象。。谢谢成功了,谢谢你的回答。之后我需要进一步处理。比如从客户那里移除物品。如果我只写var而不使用cast,那么Remove方法会抛出异常。