将 DateTime 从 IQueryable 投影到自定义类中的字符串
我使用 WCF Ria 服务在我的 Silverlight 应用程序的服务器部分上运行此代码:
public IQueryable<PersonePM> GetPersoneByCognome(string cognome)
{
return
(from p in ObjectContext.Persone
where p.Cognome.ToLower().Contains(cognome.Trim().ToLower())
select new PersonePM
{
Id = p.ID,
Cognome = p.Cognome,
Nome = p.Nome,
Sesso = p.IsMaschio == true ? "M" : "F",
StringaCognomeNome = p.Cognome + " " + p.Nome,
DataNascita = p.DataNascita == null ? DateTime.MinValue : p.DataNascita.Value,
LuogoNascita = (p.IsMaschio == true ? "Nato a " : "Nata a ") + p.Citta.Denominazione + " (" + p.Citta.Provincia.Trim() + ")",
CodiceFiscale = p.CodiceFiscale,
StringaNascita =
(p.IsMaschio == true ? "Nato a " : "Nata a ") + p.Citta.Denominazione + " (" + p.Citta.Provincia.Trim() + ")"
+
(p.DataNascita != null ?
(((p.DataNascita.Value.Day == 1) || (p.DataNascita.Value.Day == 8) || (p.DataNascita.Value.Day == 11)) ? " l'" : " il ") +
p.DataNascita.Value : string.Empty)
});
}
public class PersonePM
{
[Key]
public Guid Id { get; set; }
public string Cognome { get; set; }
public string Nome { get; set; }
public string Sesso { get; set; }
public string StringaCognomeNome { get; set; }
public DateTime DataNascita { get; set; }
public string LuogoNascita { get; set; }
public string StringaNascita { get; set; }
public string CodiceFiscale { get; set; }
}
由于意大利语,我想以通用语言形式格式化一个人的出生地点和时间,以便最好的用户理解。 但是上面的代码不起作用,因为 Linq-to-Entities 无法将 DateTime 转换为 String (整个故事有点不同......但我们简单说一下);这里抛出错误:
StringaNascita =
(p.IsMaschio == true ? "Nato a " : "Nata a ") + p.Citta.Denominazione + " (" + p.Citta.Provincia.Trim() + ")"
+
(p.DataNascita != null ?
(((p.DataNascita.Value.Day == 1) || (p.DataNascita.Value.Day == 8) || (p.DataNascita.Value.Day == 11)) ? " l'" : " il ") +
p.DataNascita.Value : string.Empty)
这个问题是众所周知的,我找到了不同的解决方案,但没有人像我用作演示模型的那样投影到自定义类上。 我已经研究这个问题大约一周了,但还没有找到解决方案。 有什么想法吗?
谢谢你!
编辑 Jul 19 16.27GMT+1
如果我注释掉这部分,
StringaNascita =
(p.IsMaschio == true ? "Nato a " : "Nata a ") + p.Citta.Denominazione + " (" + p.Citta.Provincia.Trim() + ")"
+
(p.DataNascita != null ?
(((p.DataNascita.Value.Day == 1) || (p.DataNascita.Value.Day == 8) || (p.DataNascita.Value.Day == 11)) ? " l'" : " il ") +
p.DataNascita.Value : string.Empty)
一切都会正常。
I have this code running on the server part of my Silverlight application with WCF Ria Services:
public IQueryable<PersonePM> GetPersoneByCognome(string cognome)
{
return
(from p in ObjectContext.Persone
where p.Cognome.ToLower().Contains(cognome.Trim().ToLower())
select new PersonePM
{
Id = p.ID,
Cognome = p.Cognome,
Nome = p.Nome,
Sesso = p.IsMaschio == true ? "M" : "F",
StringaCognomeNome = p.Cognome + " " + p.Nome,
DataNascita = p.DataNascita == null ? DateTime.MinValue : p.DataNascita.Value,
LuogoNascita = (p.IsMaschio == true ? "Nato a " : "Nata a ") + p.Citta.Denominazione + " (" + p.Citta.Provincia.Trim() + ")",
CodiceFiscale = p.CodiceFiscale,
StringaNascita =
(p.IsMaschio == true ? "Nato a " : "Nata a ") + p.Citta.Denominazione + " (" + p.Citta.Provincia.Trim() + ")"
+
(p.DataNascita != null ?
(((p.DataNascita.Value.Day == 1) || (p.DataNascita.Value.Day == 8) || (p.DataNascita.Value.Day == 11)) ? " l'" : " il ") +
p.DataNascita.Value : string.Empty)
});
}
public class PersonePM
{
[Key]
public Guid Id { get; set; }
public string Cognome { get; set; }
public string Nome { get; set; }
public string Sesso { get; set; }
public string StringaCognomeNome { get; set; }
public DateTime DataNascita { get; set; }
public string LuogoNascita { get; set; }
public string StringaNascita { get; set; }
public string CodiceFiscale { get; set; }
}
Because of Italian language, I'd like to format where a person is born and when in a common language form for best users comprehension.
But the code above doesen't work because Linq-to-Entities is not capable of trasform a DateTime to a String (the whole story is a little different.. but let's say that for short); the error is thrown here:
StringaNascita =
(p.IsMaschio == true ? "Nato a " : "Nata a ") + p.Citta.Denominazione + " (" + p.Citta.Provincia.Trim() + ")"
+
(p.DataNascita != null ?
(((p.DataNascita.Value.Day == 1) || (p.DataNascita.Value.Day == 8) || (p.DataNascita.Value.Day == 11)) ? " l'" : " il ") +
p.DataNascita.Value : string.Empty)
The problem is well known and I found diffrent solutions but no one with projecting on a custom class like the one I use as a presentation model.
It's about a week I'm working on this problem and I haven't figured out a solution yet.
Any ideas?
Thank you!
EDIT Jul 19 16.27GMT+1
If I comment out this part
StringaNascita =
(p.IsMaschio == true ? "Nato a " : "Nata a ") + p.Citta.Denominazione + " (" + p.Citta.Provincia.Trim() + ")"
+
(p.DataNascita != null ?
(((p.DataNascita.Value.Day == 1) || (p.DataNascita.Value.Day == 8) || (p.DataNascita.Value.Day == 11)) ? " l'" : " il ") +
p.DataNascita.Value : string.Empty)
everything works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议你从数据库中获取原始数据,然后再进行字符串转换。
要强制从
IQueryable
转换为IEnumerable
,您可以使用AsEnumerable()
- 所以你会:如果你明白我的意思。
请注意,
AsEnumerable()
实际上只是更改表达式的编译时类型,以便使用Enumerable.*
而不是执行查询的其他位可查询。*
。I would suggest you get the raw data from the database, and then perform the string conversion later.
To force a conversion from
IQueryable<T>
toIEnumerable<T>
you can useAsEnumerable()
- so you'd have:if you see what I mean.
Note that
AsEnumerable()
really just changes the compile-time type of the expression, so that the other bits of the query are performed usingEnumerable.*
instead ofQueryable.*
.现在我解决了我要问的问题:这个问题太愚蠢了没有人回答还是我是一个天才?我认为这是第一!感谢每一位帮助我解决问题的人。
解决方案
Now I solved the problem what I'm asking is: was that question too stupid no one reply or am I a sort of genius? I think it's the first! Thanks to every one helped me to solve the problem.
Solution