简体   繁体   中英

Convert SQL query to LINQ #2

I have a database includes two tables: User( idUser, firstName, lastName) and Documents( idDoc, title, expirationDate, UserDoc) where UserDoc is foreign key to idUser

Besides, I have two classes in my C# code:

 public class DocumentUI
{
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime ExpirationDate { get; set; }
    public int UserDoc { get; set; }
    public UserUI User { get; set; }
}

and

 public class UserUI
{
    public int IdUser { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

}

I need to convert below query:

select a.*, (select u.lastName from Users as u where u.idUser = a.UserDoc),(select u.firstName from Users as u where u.idUser = a.UserDoc)

from Documents as a where getdate() <= a.expirationDate

I tried and this is resutl of it:

            var documents = (from d in DocumentDAO.GetDocument()
                             join k in UserDAO.GetUsers()
                             on d.UserDoc equals k.IdUser
                             where (DateTime.Now <= d.ExpirationDate)
                             select new DocumentUI
                             {
                                 Title = d.Title,
                                 Description = d.Description,
                                 DateOfAdd = d.DateOfAdd,
                                 ExpirationDate = d.ExpirationDate,
                                 UserDoc = d.UserDoc,
                                 User = new UserUI { FirstName = k.FirstName, LastName = k.LastName}

                             }).ToList();

but in debugger I noticed that it hadn't seen any documents (I check and there is one which should be return)

I tried in this way too:

            var documents = (from d in DocumentDAO.GetDocument()
                             from k in UserDAO.GetUsers().Where(k =>k.IdUser == d.UserDoc
                             where (DateTime.Now <= d.ExpirationDate)
                             select new DocumentUI
                             {
                                 Title = d.Title,
                                 Description = d.Description,
                                 DateOfAdd = d.DateOfAdd,
                                 ExpirationDate = d.ExpirationDate,
                                 UserDoc = d.UserDoc,
                                 User = new UserUI { FirstName = k.FirstName, LastName = k.LastName}

                             }).ToList();

but it gives me the same result

And third way:

            var documents = (from d in DocumentDAO.GetDocument()
                             from k in UserDAO.GetUsers()
                             where ((DateTime.Now <= d.ExpirationDate) && (k.IdUser == d.UserDoc))
                             select new DocumentUI
                             {
                                 Title = d.Title,
                                 Description = d.Description,
                                 DateOfAdd = d.DateOfAdd,
                                 ExpirationDate = d.ExpirationDate,
                                 UserDoc = d.UserDoc,
                                 User = new UserUI { FirstName = k.FirstName, LastName = k.LastName}

                             }).ToList();

and still nothing, but if I delete (k.IdUser == d.UserDoc) it shows me this one document with all Users

UPDATE Using your opinion and ideas i wrote this:

 var now = DateTime.Now;
        var documents = DocumentDAO.GetDocument()
                    .Where(d => d.ExpirationDate > now)
                    .Select(d => new DocumentUI
             {
                 Title = d.Title,
                 Description = d.Description,
                 DateOfAdd = d.DateOfAdd,
                 ExpirationDate = d.ExpirationDate,
                 UserDoc = d.UserDoc,
                 User = new UserUI {
                     FirstName =UserDAO.GetUsers().First().FirstName,
                     LastName = UserDAO.GetUsers().First().LastName
                           }
             }).ToList(); 

but still I dont check if UserDoc equals IdUser. Where should I add this?

So you should have something like:

var result = DocumentDAO.Where(d => d.expirationDate >= DateTime.Now).Select(d =>
                     new {
                       Document = d,
                       User = UserDAO.FirstOrDefault(u => u.idUser = d.UserDoc),
                       FirstName = User.firstName,
                       LastName = User.lastName,  
                     });

If you are using EF as an ORM framework and configured proper relationships between entities you should probably have User property in Document . In that case it will be even easier:

var result = context.Documents.Where(d => d.expirationDate >= DateTime.Now).Select(d =>
                         new {
                           Document = d,
                           FirstName = d.User.firstName,
                           LastName = d.User.lastName,  
                         });
vat now = DateTime.Now;
var result = DocumentDAO.GetDocument()
                        .Where(d => d.ExpirationDate > now)
                        .Select(d => new {
                                           doc = d,
                                           firstname = d.User.FirstName,
                                           lastname = d.User.LastName
                                       });

Update

In order to return a list of DocumentUI replace the Select() statement with the following

.Select(d => new DocumentUI
                 {
                     Title = d.Title,
                     Description = d.Description,
                     DateOfAdd = d.DateOfAdd,
                     ExpirationDate = d.ExpirationDate,
                     UserDoc = d.UserDoc,
                     User = new UserUI { 
                                   FirstName = d.User.FirstName, 
                                   LastName = d.user.LastName
                               }
                 }).ToList(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM