[英]Convert SQL query to LINQ #2
我有一個數據庫包含兩個表: 用戶(ID用戶,名字,姓氏)和文件(idDoc,標題,到期日期,UserDoc),其中UserDoc是外鍵ID用戶
此外,我的C#代碼中有兩個類:
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; }
}
和
public class UserUI
{
public int IdUser { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
我需要轉換以下查詢:
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)
從文檔中獲取,其中getdate()<= a.expirationDate
我試過了,這是它的結果:
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();
但是在調試器中,我注意到它沒有看到任何文檔(我檢查了一下,應該返回其中一個)
我也嘗試過這種方式:
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();
但這給了我相同的結果
第三種方式:
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();
還是什么都沒有,但是如果我刪除(k.IdUser == d.UserDoc),它將向我顯示此文檔以及所有用戶
更新使用您的意見和想法,我這樣寫:
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();
但我仍然不檢查UserDoc是否等於IdUser。 我應該在哪里添加?
因此,您應該具有以下內容:
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,
});
如果將EF用作ORM框架並配置了實體之間的適當關系,則您可能應該在Document
具有User
屬性。 在這種情況下,它將更加容易:
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
});
更新
為了返回DocumentUI
的列表,請使用以下內容替換Select()
語句:
.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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.