Unable to cast a list
c#, list
Solution
The problem is the `new { value = ... }`
Replace:
Select(x => new {value=x.vehicleNumber+":"+x.shiftCompletedOn }).Cast<string>()
with
Select(x => x.vehicleNumber+":"+x.shiftCompletedOn)
and you're sorted. You won't need the `Cast<string>()` at all.
Your original code creates, for each record, a new instance of an anonymous type that has a member called `value` with the string desired; the second version just creates the string.
In a way, it is no different to trying this:
class Foo
{
public string Bar {get;set;}
}
...
var foo = new Foo { Bar = "abc" };
string s = (string)foo; // doesn't compile
Problem
I am getting a error in the line below. ``` 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()); ``` Th error Message being ``` Unable to cast object of type '<>f__AnonymousType0`1[System.String]' to type 'System.String'. ``` The list day1 is of type ``` 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(); } } } ``` What exactly Am i doing wrong??