Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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#_List - Fatal编程技术网

C# 无法强制转换列表

C# 无法强制转换列表,c#,list,C#,List,我在下面的行中得到一个错误 temp.day1_veh_p = string.Join(Environment.NewLine, day1.Where(x => x.plannedTriips == 1).Select(x => new {value=x.vehicleNumber+":"+x.shiftCompletedOn }).Cast<string>().ToArray()); temp.day1\u veh\u p=string.Join(Environme

我在下面的行中得到一个错误

 temp.day1_veh_p = string.Join(Environment.NewLine, day1.Where(x => x.plannedTriips == 1).Select(x => new {value=x.vehicleNumber+":"+x.shiftCompletedOn }).Cast<string>().ToArray());
temp.day1\u veh\u p=string.Join(Environment.NewLine,day1.Where(x=>x.plannedTriips==1)。选择(x=>new{value=x.vehicleNumber+”:“+x.shiftCompletedOn})。Cast().ToArray());
正在发送第条错误消息

Unable to cast object of type '<>f__AnonymousType0`1[System.String]' to type 'System.String'.
无法将类型为“f_uuAnonymousType0`1[System.String]”的对象强制转换为类型为“System.String”。
列表day1的类型为

public class tripDetails
{
    public string accountID { get; set; }
    public string supplierName { get; set; }
    public string supplierCode { get; set; }
    public DateTime shiftFrom { get; set; }
    public DateTime shiftTo { get; set; }
    public int plannedTriips { get; set; }
    public int actualTrips { get; set; }
    public DateTime forDate { get; set; }
    public string vehicleNumber { get; set; }
    public string shiftCompletedOn { get; set; }
    public class Comparer : IEqualityComparer<tripDetails>
    {
        public bool Equals(tripDetails x, tripDetails y)
        {
            return x.supplierCode == y.supplierCode;
        }

        public int GetHashCode(tripDetails obj)
        {
            return (obj.supplierCode).GetHashCode();
        }
    }
}
公共类详细信息
{
公共字符串accountID{get;set;}
公共字符串供应商名称{get;set;}
公共字符串供应商代码{get;set;}
来自{get;set;}的公共日期时间移位
公共日期时间移位到{get;set;}
公共整数规划triips{get;set;}
public int actualTrips{get;set;}
日期{get;set;}的公共日期时间
公共字符串vehicleNumber{get;set;}
{get;set;}上的公共字符串移位完成
公共类比较器:IEqualityComparer
{
公共布尔等于(tripDetails x,tripDetails y)
{
返回x.supplierCode==y.supplierCode;
}
公共int GetHashCode(tripDetails obj)
{
return(obj.supplierCode).GetHashCode();
}
}
}

我到底做错了什么???

是的,匿名类型不是字符串,所以替换它

 .Select(x => new { value = x.vehicleNumber + ":" + x.shiftCompletedOn })
(x => new {value=x.vehicleNumber+":"+x.shiftCompletedOn }).Cast<string>()

然后可以使用
string.Join的查询(不需要创建新数组)

使用多行代码也很有帮助,它使您的代码更具可读性:

var vehicles = day1.Where(x => x.plannedTriips == 1)
              .Select(x => x.vehicleNumber + ":" + x.shiftCompletedOn);
string str = string.Join(Environment.NewLine, vehicles);
替换这个

 .Select(x => new { value = x.vehicleNumber + ":" + x.shiftCompletedOn })
(x => new {value=x.vehicleNumber+":"+x.shiftCompletedOn }).Cast<string>()

当您在执行
new{…}
时,您正在创建匿名类型的项,然后(
Cast问题是
new{value=…}

替换:

Select(x => new {value=x.vehicleNumber+":"+x.shiftCompletedOn }).Cast<string>()
您就可以排序了。您根本不需要
Cast()

原始代码为每个记录创建一个匿名类型的新实例,该实例有一个名为
value
的成员,其中包含所需的字符串;第二个版本只创建该字符串

在某种程度上,尝试以下方法也没什么不同:

class Foo
{
    public string Bar {get;set;}
}
...
var foo = new Foo { Bar = "abc" };
string s = (string)foo; // doesn't compile
class Foo
{
    public string Bar {get;set;}
}
...
var foo = new Foo { Bar = "abc" };
string s = (string)foo; // doesn't compile