我有一个包含两个表的数据库:用户(idUser,firstName,lastName)和 文档(idDoc,title,expirationDate,UserDoc)其中UserDoc是 idUser
的外键此外,我的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)
来自Documents的其中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。我应该在哪里添加这个?
答案 0 :(得分:1)
所以你应该有类似的东西:
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 框架并在实体之间配置了正确的关系,那么您应该在User
中拥有Document
属性。在这种情况下,它会更容易:
var result = context.Documents.Where(d => d.expirationDate >= DateTime.Now).Select(d =>
new {
Document = d,
FirstName = d.User.firstName,
LastName = d.User.lastName,
});
答案 1 :(得分:1)
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();