我想从我的表中生成一个XDocument(结构不同),但我不断收到以下错误:LINQ to Entities无法识别方法'System.String ToString()'。

我知道这是由Birthdate引起的,我需要使用SqlFunctions.StringConvert,但我正在使用Framework 4.0

关于如何解决的任何想法?

// Newsletter subscriptions
var news = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("USER",
            from n in _dc.Newsletter_Datas
            where n.Timestamp >= startDate
            select
               new XElement("ROW",
                    new XElement("UTI", n.ID),
                    new XElement("FIRSTNAME", n.FirstName),
                    new XElement("FAMILYNAME", n.LastName),
                    new XElement("GENDER", n.Gender),
                    new XElement("BIRTHDATE", n.BirthDate != null ? n.BirthDate.Value.ToString("yyyy-MM-dd") : "2003-01-01"),
                    new XElement("LANGUAGE", n.Language),
                    new XElement("EMAIL", n.Email),
                    new XElement("STREETNAME", n.StreetName),
                    new XElement("HOUSENR", n.HouseNr),
                    new XElement("BOXNR", n.BoxNr),
                    new XElement("ZIPCODE", n.Zipcode),
                    new XElement("CITY", n.City),
                    new XElement("COUNTRY", n.Country),
                    new XElement("TS", n.Timestamp.ToString("yyyy-MM-dd hh:mm:ss")),
                    new XElement("MESSAGE_ID", "SCNEWS02"),
                    new XElement("OPT_INS",
                        new XElement("OPT_IN",
                            new XElement("OPT_IN_CBP", "01000000000"),
                            new XElement("OPT_IN_INSERT_TS", n.Timestamp.ToString("yyyy-MM-dd hh:mm:ss")),
                            new XElement("OPT_IN_METHOD", "1"),
                            new XElement("OPT_IN_TYPE", n.OptIn),
                            new XElement("OPT_IN_CHANNEL", "2"))))));

非常感谢你

===============>>#1 票数:1 已采纳

将查询分为几个部分,第一部分是查询本身。 这将由实体框架处理。 然后将该查询转换为IEnumerable<NewsletterData>以使用LINQ对对象完成此查询。 实体框架只能做一个简单的投影(您显然没有做)。

var query =
    from n in _dc.Newsletter_Datas
    where n.Timestamp >= startDate
    select n;
var news = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
    new XElement("USER",
        from n in query.AsEnumerable() // LINQ to objects
        select new XElement("ROW",
            new XElement("UTI", n.ID),
            new XElement("FIRSTNAME", n.FirstName),
            new XElement("FAMILYNAME", n.LastName),
            new XElement("GENDER", n.Gender),
            new XElement("BIRTHDATE", n.BirthDate != null ? n.BirthDate.Value.ToString("yyyy-MM-dd") : "2003-01-01"),
            new XElement("LANGUAGE", n.Language),
            new XElement("EMAIL", n.Email),
            new XElement("STREETNAME", n.StreetName),
            new XElement("HOUSENR", n.HouseNr),
            new XElement("BOXNR", n.BoxNr),
            new XElement("ZIPCODE", n.Zipcode),
            new XElement("CITY", n.City),
            new XElement("COUNTRY", n.Country),
            new XElement("TS", n.Timestamp.ToString("yyyy-MM-dd hh:mm:ss")),
            new XElement("MESSAGE_ID", "SCNEWS02"),
            new XElement("OPT_INS",
                new XElement("OPT_IN",
                    new XElement("OPT_IN_CBP", "01000000000"),
                    new XElement("OPT_IN_INSERT_TS", n.Timestamp.ToString("yyyy-MM-dd hh:mm:ss")),
                    new XElement("OPT_IN_METHOD", "1"),
                    new XElement("OPT_IN_TYPE", n.OptIn),
                    new XElement("OPT_IN_CHANNEL", "2")
                )
            )
        )
    )
);

  ask by Hervé Donner translate from so

未解决问题?本站智能推荐: