List<Post> list =
(
from c in db.TitleComments
join t in db.Titles on c.TitleId equals t.Id
join u in db.Users on c.UserId equals u.Id
where t.Id == _titleId && c.Date > time
orderby c.Date descending
select new Post { Username = u.Username, PostingDate = c.Date.ToString(), Data = c.Comment }
).ToList();
Run Code Online (Sandbox Code Playgroud)
上面的代码导致date转换为string,PostingDate = c.Date.ToString().任何想法如何解决这个问题?
异常错误:{"LINQ to Entities无法识别方法'System.String ToString()'方法,并且此方法无法转换为商店表达式."}
我有两节课:
class Foo{
public int FooId { get; set; }
...
public Bar Bar { get; set }
}
class Bar{
public int BarId { get; set; }
public int FooId { get; set }
...
}
Run Code Online (Sandbox Code Playgroud)
当我然后运行这样的查询:
sqlConnection.Query<Foo, Bar, Foo>(
"SELECT * FROM Foo JOIN Bar ON Foo.FooId = Bar.FooId",
(foo, bar) => {
foo.Bar = bar;
return foo;
},
splitOn: "FooId");
Run Code Online (Sandbox Code Playgroud)
结果将是Foo和Bar上的所有属性都将映射除了Bar.BarId.检查列名并在我的Bar类中键入数据库后,我仍然找不到任何差异.
我偶然发现的一件奇怪的事情是,如果我写道:
"SELECT *, BarId AS BarId FROM Foo JOIN Bar ON Foo.FooId = Bar.FooId"
Run Code Online (Sandbox Code Playgroud)
Bar.BarId实际上按预期映射,我误解了如何使用Dapper或者这是一个错误吗?