I have 3 classes, DOb, Patient, and LookUp
public class Dog
{ public int Id { get; set; }
public DateTime DOB { get; set; }
public string FirstName { get; set; }
public string Display { get; set; }
public int qqQMS { get; set; }
public string Description { get; set; }
}
public class Patient
{
public int Id { get; set; }
public Guid Patid { get; set; }
public string FirstName { get; set; }
public string Display { get; set; }
public string Description { get; set; }
}
public class LookUp : ILookUp
public int Id { get; set; }
public string Display { get; set; }
public string Description { get; set; }
}
I am trying to easily take a list or List convert it to List I asked this question before and never figured it out, with out using a Dynamic, I would rather have a smoother way without Dynamics
I have tried
List<LookUp> Look = doggy.Cast<LookUp>().ToList();
List<LookUp> patLook = pat.Cast<LookUp>().ToList();
also tried this but I am not happy with it
public static IList<LookUp> ToLookUp<T>( IEnumerable<T> source)
{
List<LookUp> list = (from prop in source
select new LookUp
{
Display = prop.Display ?? "",
Description = prop.Description ?? "",
Id = prop.Id
}).ToList();
return list;
}
an extension method could work
You will need to implement the interface on your classes , the concrete Lookup
class is not needed at this point:
public class Dog : ILookUp { ... }
public class Patient : ILookUp { ... }
That way you can cast to your interface and use it:
var listOfDogs = new List<dog>();
var LookupList = listOfDogs.Cast<ILookup>();
I would love to but Dob ad Patient are created else where and I cannot modify them
Then there is no easier way to do this, you can't really make a generic solution without reflection, you will have to project to Lookup
as you are doing, or make an expression for each type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.