2012-11-06 11 views
5

Verilerle birlikte bir view var ve veriyi yalnızca son 7 gün içinde almam gerekiyor. Sql sorgu kullanıyor olsaydı bunun için bir işlev olduğunu biliyorum. ama Linq kullanıyorum. Burada Veritabanından son 7 gün içinde veri alınıyor linq

benim kodudur:

Bu Linq nasıl
   try 
       { 
       var query = (from bwl in mg.BarcodeWithLocation 
          select new 
          { 
           RequestID = bwl.RequestID, 
           Barcode = bwl.Barcode, 
           adrid = bwl.AdrID, 
           name = bwl.Name, 
           street = bwl.Street, 
           houseno = bwl.HouseNo, 
           postal = bwl.Postal, 
           city = bwl.City, 
           country = bwl.Country, 
           latitudetxt = bwl.Latitude == "" ? "Location Unknown" : "View Map Location", 
           latitude = bwl.Latitude, 
           longitude = bwl.Longitude, 
           date = bwl.ReceivedDate 
          }); 

       this.Grid.DataSource = query; 
       this.Grid.DataBind(); 
       } 
       catch (Exception exception) 
       { 
         Console.WriteLine("ERROR in GetNoLocationScan() method. Error Message : " + exception.Message); 
       } 

kimse bana söyleyebilir?

cevap

8

Kayıt işlemini yedi gün önce ve ardından geçerli tarihi almak için DateTime.Now.AddDays(-7) kullanabilirsiniz. Veya zaman bölümünü istiyorsanız ve 12: 00'dan sonra başlıyorsanız, Datime.Today.AddDays (-7) 'yi kullanabilirsiniz.

var query = (from bwl in mg.BarcodeWithLocation 
       where(bwl.ReceivedDate > DateTime.Now.AddDays(-7)) 
         select new 
         { 
          RequestID = bwl.RequestID, 
          Barcode = bwl.Barcode, 
          adrid = bwl.AdrID, 
          name = bwl.Name, 
          street = bwl.Street, 
          houseno = bwl.HouseNo, 
          postal = bwl.Postal, 
          city = bwl.City, 
          country = bwl.Country, 
          latitudetxt = bwl.Latitude == "" ? "Location Unknown" : "View Map Location", 
          latitude = bwl.Latitude, 
          longitude = bwl.Longitude, 
          date = bwl.ReceivedDate 
         }); 
+1

Sana Varlık – Uriil

+0

Veya 'DateTime.Today.AddDays (-7)' LINQ içine DateTime.Now.AddDays (-1) kullanamazsınız düşünüyorum Zamana göre filtrelemek istemiyorsanız –

+0

@Adil Bu sorgu datetime ile çalışıyor mu? –

1

bu deneyin:

var dt = DateTime.Now.AddDays(-7); 
    var query = (from bwl in mg.BarcodeWithLocation 
       where bwl.ReceivedDate > dt 
         select new 
         { 
          RequestID = bwl.RequestID, 
          Barcode = bwl.Barcode, 
          adrid = bwl.AdrID, 
          name = bwl.Name, 
          street = bwl.Street, 
          houseno = bwl.HouseNo, 
          postal = bwl.Postal, 
          city = bwl.City, 
          country = bwl.Country, 
          latitudetxt = bwl.Latitude == "" ? "Location Unknown" : "View Map Location", 
          latitude = bwl.Latitude, 
          longitude = bwl.Longitude, 
          date = bwl.ReceivedDate 
         }); 
İlgili konular