-3

I have problem with that query:

    select SanPham.*,TenSX  
    from SanPham join NhaSanXuat on SanPham.MaSX = NhaSanXuat.MaSX 
    where GETDATE()- NgayNhap < 15
    order by NgayNhap,MaSP desc

I seem to have a problem with : where getdate() - Ngaynhap < 15

This is the linq query :

var sp = db.SanPhams.Join(db.NhaSanXuats, masp => masp.MaSX, mansx => mansx.MaSX, 
(masp, mansx) => new { SanPham = masp, NhaSanXuat = mansx })
.Where(ngaynhap=>DateTime.Now.Day - db.SanPhams.Select(n=>n.NgayNhap.Value.Day))
.OrderByDescending(nhap => nhap.SanPham.NgayNhap)
.ThenByDescending(masp => masp.SanPham.MaSP)
.Select(s => s.SanPham);
6
  • 2
    How can we help when you haven't explained the problem? You haven't said what's going wrong.
    – Jon Skeet
    Jun 26, 2013 at 13:22
  • It seems he is asking how to convert the first query into the second one and he is having trouble with the where clause. Jun 26, 2013 at 13:29
  • i have trouble with the where clause, how write if((datetime.now- db.sanpham.ngay)<15)
    – lee trj
    Jun 26, 2013 at 13:35
  • sorry i write english very bad, I wish you understood 12 years
    – lee trj
    Jun 26, 2013 at 13:38
  • What type is NgayNhap? Jun 26, 2013 at 13:39

1 Answer 1

0

Without slightly more information it would be hard to help you better. But I think you are looking for the following:

var sp = db.SanPhams.Join(db.NhaSanXuats, masp => masp.MaSX, mansx => mansx.MaSX, 
(masp, mansx) => new { SanPham = masp, NhaSanXuat = mansx })
.Where(db => (DateTime.Now - db.SanPham.NgayNhap).Days < 15)
.OrderByDescending(nhap => nhap.SanPham.NgayNhap)
.ThenByDescending(masp => masp.SanPham.MaSP)
.Select(s => s.SanPham);
4
  • QLMobileDataContext db = new QLMobileDataContext();
    – lee trj
    Jun 26, 2013 at 13:43
  • i have error Error" 'System.Linq.IQueryable<AnonymousType#1>' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Enumerable.Where<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,int,bool>)' has some invalid argument"
    – lee trj
    Jun 26, 2013 at 13:44
  • If you are using Entity Framework then you shouldn't need to join. And I actually asked what type is NgayNhap which i am currently assuming is System.DateTime Jun 26, 2013 at 13:48
  • I just did a small test and it worked fine with standard types so you definitely need to check what the type of your property is. Jun 26, 2013 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.