Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
数据类型linq中存在错误_Linq - Fatal编程技术网

数据类型linq中存在错误

数据类型linq中存在错误,linq,Linq,有一个请求: “带上两个年龄较大(最大年龄)的孩子,完成他们在幼儿园的停留” “生日”-数据类型=>“日期时间” 错误:DbathmeticExpression参数必须具有数字公共类型 我哪里出错了 EF不支持对DateTime进行算术运算 var s = db.Child.Max(e => EntityFunctions.DiffDays(DateTime.Today - e.Birthday)); 你把事情复杂化了。最大的孩子是。。。他们是第一个出生的 所以 从foreach语句中提

有一个请求: “带上两个年龄较大(最大年龄)的孩子,完成他们在幼儿园的停留”

“生日”-数据类型=>“日期时间”

错误:DbathmeticExpression参数必须具有数字公共类型


我哪里出错了

EF不支持对DateTime进行算术运算

var s = db.Child.Max(e => EntityFunctions.DiffDays(DateTime.Today - e.Birthday));

你把事情复杂化了。最大的孩子是。。。他们是第一个出生的

所以


从foreach语句中提取where语句并检查数据库数据类型

class Program
{
    static void Main(string[] args)
    {
            List<Test> Tests = new List<Test>();
            Tests.Add(new Test(new DateTime(2014, 01, 01)));
            Tests.Add(new Test(new DateTime(2013, 01, 01)));
            Tests.Add(new Test(new DateTime(2012, 01, 01)));
            Tests.Add(new Test(new DateTime(2011, 01, 01)));
            Tests.Add(new Test(new DateTime(2010, 01, 01)));
            Tests.Add(new Test(new DateTime(2009, 01, 01)));
            Tests.Add(new Test(new DateTime(2008, 01, 01)));


            var s = Tests.Max(e => (DateTime.Today - e.Birthday));
            var d =Tests.Where(e => (DateTime.Today - e.Birthday) == s).Take(2);

    }
}

public class Test
{
    public DateTime Birthday { get; set; }

    public Test(DateTime dateTime)
    {
        Birthday = dateTime;
    }
}

s
的类型是什么?除非它是一个
TimeSpan
,否则我不会期望
Where
子句有多大意义……非常感谢。但是,什么是错误“System.Data.EntityCommandExecutionException”?它在DeleteObject上吗?您可能需要改用
db.Child.DeleteObject(Child)
var oldestChildren = db.Child.OrderBy(c => c.Birthday).Take(2).ToList();

//if "complete they stay in kindergarten means delete
foreach (var child in oldestChildren)
   db.DeleteObject(child);
class Program
{
    static void Main(string[] args)
    {
            List<Test> Tests = new List<Test>();
            Tests.Add(new Test(new DateTime(2014, 01, 01)));
            Tests.Add(new Test(new DateTime(2013, 01, 01)));
            Tests.Add(new Test(new DateTime(2012, 01, 01)));
            Tests.Add(new Test(new DateTime(2011, 01, 01)));
            Tests.Add(new Test(new DateTime(2010, 01, 01)));
            Tests.Add(new Test(new DateTime(2009, 01, 01)));
            Tests.Add(new Test(new DateTime(2008, 01, 01)));


            var s = Tests.Max(e => (DateTime.Today - e.Birthday));
            var d =Tests.Where(e => (DateTime.Today - e.Birthday) == s).Take(2);

    }
}

public class Test
{
    public DateTime Birthday { get; set; }

    public Test(DateTime dateTime)
    {
        Birthday = dateTime;
    }
}
var oldestChildren = db.Child.OrderBy(c => c.Birthday).Take(2).ToList();