English 中文(简体)
B. 在Linq内部对实体的操纵日期
原标题:DateTime manipulation inside Linq to entities query

操作该法典:

public bool CheckTime(DateTime date, int UserID, int TotalTimeMin)
        {
            using (var context = new myDB())
            {         
                var assginments = from c in context.Assignments.Where(x=>(x.AssignmentDateTime < date && x.AssignmentDateTime.Value.AddMinutes(TotalTimeMin) > date) || 
                    (x.AssignmentDateTime < date.AddMinutes(TotalTimeMin))) select c;

                if(assginments != null) return false;
                else return true;
            }


        }

我发现这一错误。

LINQ to Entities does not recognize the method  System.DateTime AddMinutes(Double)  method, and this method cannot be translated into a store expression.

TotalTimeMin is int. I am not sure what cause this: AssignmentDateTime is DateTime? and maybe this is the problem ?

最佳回答

使用<代码>意向功能。

    public bool CheckTime(DateTime date, int UserID, int TotalTimeMin)
    {
        using (var context = new myDB())
        {         
            var assginments = context.Assignments
                                     .Where(x=>(x.AssignmentDateTime < date 
                                                && EntityFunctions.AddMinutes(x.AssignmentDateTime,TotalTimeMin) > date) 
                                                || (x.AssignmentDateTime < date.AddMinutes(TotalTimeMin)));

            if(assginments != null) return false;
            else return true;
        }
    }

请注意,<条码>签字>将失效(但可以是空洞的<条码>签字>。

问题回答

暂无回答




相关问题
LINQ Training: Native SQL Queries to LINQ

Can anyone point out some good LINQ training resources. Mostly interested in getting some developers that are very skilled with SQL up to speed using Lambda and LINQ queries. They are struggling with ...

How can i write Linq2Entities Query with inner joins

How can I get data from these related entities. I want to get these columns only: Term.Name , related Concept_Term.Weight, related Concept.Id I wrote the SQL but I don t want to use select t....

Linq to enties, insert foreign keys

I am using the ADO entity framework for the first time and am not sure of the best way of inserting db recored that contain foreign keys. this is the code that i am using, I would appreciate any ...

Linq to entity timing problem

I created a data model that has some views from my database as entities,when I create a linq statment that has part of this view s entities ,the time processor take to execute the linq statment is ...

热门标签