[英]Error getting when trying assign linq data into DTO class
我需要將 Linq 數據分配到我的 DTO class 中,但是這個 Linq 查詢得到了這個錯誤。
LINQ to Entities does not recognize the method 'System.String ToString(System.String, System.IFormatProvider)' method, and this method cannot be translated into a store expression.
我刪除了 int 轉換以避免錯誤,然后我得到了這個錯誤
join 子句中的表達式之一的類型不正確。 類型推斷在加入呼叫中失敗
我的代碼如下,
public class TextDto
{
public string AddedData { get; set; }
public string AddedTime { get; set; }
public string Sender { get; set; }
public string Name { get; set; }
public string Messsage { get; set; }
}
linq代碼,
var allText = from nm in context.NotificationMessages
join u in context.Users on new {
Sender = (int) nm.Sender
}
equals new {
Sender = u.UserId
}
select new {
nm.Id,
nm.AddedTime,
nm.Lobby.Branch.BranchName,
u.FirstName,
u.LastName,
nm.Message
};
var allTextList = allText.Select(data = >new TextOnDemandDto {
AddedTime = data.AddedTime.ToString("hh:mm tt", CultureInfo.InvariantCulture),
Messsage = data.Message,
Name = data.FirstName + " " + data.LastName
}).ToList();
這個sql,我把它轉換成linq
SELECT nm.id,
nm.addedtime,
b.branchname,
u.firstname,
u.lastname,
nm.message
FROM notificationmessage nm
INNER JOIN lobby l
ON l.lobbyid = nm.fklobbyid
INNER JOIN branch b
ON b.branchid = l.fkbranchid
INNER JOIN [user] u
ON nm.sender = u.userid
嘗試將您的查詢更改為:
var allText = (from nm in context.NotificationMessages
join u in context.Users
on nm.Sender equals u.UserId into ug
from u in ug.DefaultIfEmpty()
join l in context.Lobbys
on nm.fklobbyid eguals l.lobbyid into lg
from l in lg.DefaultIfEmpty()
join b in context.Branches
on l.fkbranchid equals b.branchid into bg
from b in bg.DefaultIfEmpty()
select new {
nm.Id,
nm.AddedTime,
b.BranchName,
u.FirstName,
u.LastName,
nm.Message
}).ToList();
更新:
要獲取 FKNotificationMessageTypeId = 3 的記錄,請在“from b..”和“select”之間插入 where 子句:
... from b ...
where nm.FKNotificationMessageTypeId == 3
select new ....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.