Linq sql - 我应该把短语放在"其中"

时间:2013-11-29 00:20:28

标签: linq

这是我的查询,它几乎可以工作。我需要添加到此查询“where(k.IdUser == d.UserDoc。 添加此问题时会出现问题。无论我把它放在哪里,它都会停止工作。 加入不是此选项。

 var documents = (from d in DocumentDAO.GetDocument()
                         from k in UserDAO.GetUsers()
                         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();

我的应用从数据库中获取数据并在窗口中显示此数据。如果我使用此查询启动我的应用程序(我在db中只有一个文档)它向我显示相同的文档,但是对于所有用户,我希望我的应用程序只向我显示这个用户ID是文档中的外键(UserDoc)

我试过这样的事情:

var documents = (from d in DocumentDAO.GetDocument()
                     from k in UserDAO.GetUsers()
                     where ((DateTime.Now <= d.ExpirationDate) && (d.UserDoc == k.IdUser))
                     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();

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();

在这两种情况下,我的应用程序都没有显示任何内容

这是类DocumentUI:

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

1 个答案:

答案 0 :(得分:0)

如果您拥有满足List<DocumentUI>

的正确数据,则以下查询(与您的第一个查询相同)应返回((DateTime.Now <= d.ExpirationDate) && (d.UserDoc == k.IdUser))
var documents = (from d in DocumentDAO.GetDocument()
                 join k in UserDAO.GetUsers()
                 on d.UserDoc equals k.IdUser
                 where (d.ExpirationDate >= DateTime.Now)
                 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();

仍然如果你没有得到结果,请验证Var documents = DocumentDAO.GetDocument(), var users = UserDAO.GetUsers()中的值。