1

我想从我的表中生成一个 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"))))));

非常感谢

4

1 回答 1

1

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

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")
                )
            )
        )
    )
);
于 2013-04-16T02:28:59.457 回答