2008-10-18 17 views
15

मैं यह जांचना चाहता हूं कि किसी दिए गए दिनांक को LINQ का उपयोग करके आज की तारीख से एक माह पहले एक महीने पहले किया गया हो।LINQ में तिथियों की तुलना कैसे करें?

इसके लिए वाक्यविन्यास क्या है?

अग्रिम धन्यवाद।

उत्तर

29

मुझे लगता है कि आप कहां से खंड में बात कर रहे हैं। यह मूल रूप से वही तरीका है जहां आप दो डेटटाइम ऑब्जेक्ट्स की तुलना कहीं और करेंगे।

using (DataContext context = new DataContext()) { 
    var query = from t in context.table 
       where t.CreateDate.Date < DateTime.Today.AddMonths(-1) 
       select t; 
} 
5

बस LINQ से इकाइयों में जोड़ने के लिए आपको एक चर से तुलना करना होगा। उदाहरण:

 DateTime lastMonth = DateTime.Today.AddMonths(-1); 
     using (var db = new MyEntities()) 
     { 
      var query = from s in db.ViewOrTable 
         orderby s.ColName 
         where (s.StartDate > lastMonth) 
         select s; 

      _dsResults = query.ToList(); 
     } 
0

आप नहीं चाहते यदि आपका कोड फेंक

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

मैं DbFunctions.AddYears उपयोग करने का सुझाव होगा। इसके अलावा, अगर आप केवल दिनांक और समय नहीं के बारे में चिंतित हैं, तो आप इस्तेमाल कर सकते हैं DbFunctions.TruncateTime

कुछ इस-

DbFunctions.TruncateTime(x.date) == 
DbFunctions.TruncateTime(DbFunctions.AddYears(DateTime.Today, 1)) 
की तरह
संबंधित मुद्दे