1

テーブルから XDocument を生成したいのですが (構造が異なります)、次のエラーが発生し続けます: LINQ to Entities はメソッド 'System.String ToString()' メソッドを認識しません。

これは生年月日が原因であることはわかっており、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

Break your query into separate parts, the first part is the query itself. That will be handled by the entity framework. Then cast that query to an IEnumerable<NewsletterData> to finish it off using LINQ to objects. Entity Framework can't do more than a simple projection (which you are clearly not doing).

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 に答える