将 DateTime 从 IQueryable 投影到自定义类中的字符串

发布于 2024-09-10 07:04:11 字数 2926 浏览 1 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法回应 2024-09-17 07:04:11

我建议你从数据库中获取原始数据,然后再进行字符串转换。

要强制从 IQueryable 转换为 IEnumerable,您可以使用 AsEnumerable() - 所以你会:

var dbQuery = from data in source
              where stuff
              select simple-projection;

var clrQuery = from data in dbQuery.AsEnumerable()
               where client-side-filters
               select client-side-projection;

如果你明白我的意思。

请注意,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> to IEnumerable<T> you can use AsEnumerable() - so you'd have:

var dbQuery = from data in source
              where stuff
              select simple-projection;

var clrQuery = from data in dbQuery.AsEnumerable()
               where client-side-filters
               select client-side-projection;

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 using Enumerable.* instead of Queryable.*.

花心好男孩 2024-09-17 07:04:11

现在我解决了我要问的问题:这个问题太愚蠢了没有人回答还是我是一个天才?我认为这是第一!感谢每一位帮助我解决问题的人。

解决方案

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; }

    /*Use the Getter to set the property based on other fields*/
    public string StringaNascita 
    {
        get
        {
            return LuogoNascita +
                (DataNascita != DateTime.MinValue ?
                     (((DataNascita.Day == 1) || (DataNascita.Day == 8) || (DataNascita.Day == 11)) ? " l'" : " il ") +
                     string.Format("{0:d MMMM yyyy}", DataNascita) : string.Empty);
        }
    }
    /* END of solution */

    public string CodiceFiscale { get; set; }
}

public IEnumerable<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.HasValue ? p.DataNascita.Value : DateTime.MinValue,
                 LuogoNascita = (p.IsMaschio == true ? "Nato a " : "Nata a ") + p.Citta.Denominazione + " (" + p.Citta.Provincia.Trim() + ")",
                 CodiceFiscale = p.CodiceFiscale,
             });
    }

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

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; }

    /*Use the Getter to set the property based on other fields*/
    public string StringaNascita 
    {
        get
        {
            return LuogoNascita +
                (DataNascita != DateTime.MinValue ?
                     (((DataNascita.Day == 1) || (DataNascita.Day == 8) || (DataNascita.Day == 11)) ? " l'" : " il ") +
                     string.Format("{0:d MMMM yyyy}", DataNascita) : string.Empty);
        }
    }
    /* END of solution */

    public string CodiceFiscale { get; set; }
}

public IEnumerable<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.HasValue ? p.DataNascita.Value : DateTime.MinValue,
                 LuogoNascita = (p.IsMaschio == true ? "Nato a " : "Nata a ") + p.Citta.Denominazione + " (" + p.Citta.Provincia.Trim() + ")",
                 CodiceFiscale = p.CodiceFiscale,
             });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文