尝试将属性值检索为对象而不是各自的类型时,我遇到了一些麻烦.以下代码抛出此异常:
Unable to cast the type 'System.DateTime' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.
Run Code Online (Sandbox Code Playgroud)
选择字符串时此代码可以正常工作,但选择DateTimes,Integers或Nullable类型时则不行.
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreatedOn { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
using (var ctx = new MyContext())
{
// Property selector: select DateTime as Object
Expression<Func<Customer, object>> selector = cust => cust.CreatedOn;
// …Run Code Online (Sandbox Code Playgroud)