结合Linq to SQL和Linq to XML问题

时间:2017-09-15 16:08:02

标签: c# sql xml linq

我有以下工作代码,用于从XML字符串输入创建特定格式的XML文件。

            var dc = XElement.Parse(InputXml);
            var cd = ContextDate.ToString("yyyyMMdd");

            var xc = XNamespace.Get("XmlCache");
            var mp = XNamespace.Get("mx.MarketParameters");
            var fx = XNamespace.Get("mx.MarketParameters.Forex");
            var fxsp = XNamespace.Get("mx.MarketParameters.Forex.Spot");
            var xdoc = new XDocument(
                new XDeclaration("1.0", "utf-8", "no"),
                new XElement(xc + "XmlCache", new XAttribute(XNamespace.Xmlns + "xc", xc),
                    new XAttribute(XNamespace.Xmlns + "mp", mp),
                    new XAttribute(XNamespace.Xmlns + "fx", fx),
                    new XAttribute(XNamespace.Xmlns + "fxsp", fxsp),
                    new XAttribute(xc + "action", "Update"),
                    new XElement(xc + "XmlCacheArea", new XAttribute(xc + "value", "MarketParameters"),
                        new XElement(mp + "nickName", new XAttribute(XNamespace.Xmlns + "mp", mp),
                            new XAttribute(xc + "value", MpNickName),
                            new XElement(mp + "date",
                                new XAttribute(xc + "value", cd),
                                new XElement(fx + "forex", new XAttribute(XNamespace.Xmlns + "fx", fx),
                                    new XElement(fxsp + "spot",
                                        new XAttribute(XNamespace.Xmlns + "fxsp", fxsp),
                                        from myRow in dc.Elements("row").AsEnumerable()
                                        group myRow by new { ccy = myRow.GetElementValue("Ccy") }
                                        into myGroup
                                        select
                                        new XElement(fxsp + "pair",
                                            new XAttribute(xc + "value", myGroup.Key.ccy),
                                                from myRow in myGroup
                                                select
                                                new XElement(fxsp + "pair",
                                                    new XAttribute(xc + "value", myRow.GetElementValue("Identifier")),
                                                    new XAttribute(xc + "type", "Fields"),
                                                    new XElement(mp + "ask", new XAttribute(xc + "keyFormat", "N"), myRow.GetElementValue("Value")),
                                                    new XElement(mp + "bid", new XAttribute(xc + "keyFormat", "N"), myRow.GetElementValue("Value")),
                                                    new XElement(mp + "mid", new XAttribute(xc + "keyFormat", "N"), myRow.GetElementValue("Value")),
                                                    new XElement(mp + "quotation", new XAttribute(xc + "keyFormat", "C"),
                                                        myRow.GetElementValue("Identifier")))))))))));

但现在我需要在表上进行自联接以将2行组合为1.该表包含2个不同的ProductTypes,需要由Identifier列连接。 所以我使用myRow作为类型A,使用shiftRow作为类型B. 我需要将两列Value列的值放在一行中。为了克服我读到的含糊不清的列名,我需要使用如此处所述的join LINQ to SQL: How to handle ambiguous column names when joining tables? 但我不能使用选择新{t1A = t1.ColumnA,t2A = t2.ColumnA,t3A = t3.ColumnA}'正如我在LINQ to XML中所建议的那样。 我在下面尝试了但是它没有使用注释行进行编译而没有注释。

            xdoc = new XDocument(
                new XDeclaration("1.0", "utf-8", "no"),
                new XElement(xc + "XmlCache", new XAttribute(XNamespace.Xmlns + "xc", xc),
                    new XAttribute(XNamespace.Xmlns + "mp", mp),
                    new XAttribute(XNamespace.Xmlns + "fx", fx),
                    new XAttribute(XNamespace.Xmlns + "fxsp", fxsp),
                    new XAttribute(xc + "action", "Update"),
                    new XElement(xc + "XmlCacheArea", new XAttribute(xc + "value", "MarketParameters"),
                        new XElement(mp + "nickName", new XAttribute(XNamespace.Xmlns + "mp", mp),
                            new XAttribute(xc + "value", MpNickName),
                            new XElement(mp + "date",
                                new XAttribute(xc + "value", cd),
                                new XElement(fx + "forex", new XAttribute(XNamespace.Xmlns + "fx", fx),
                                    new XElement(fxsp + "spot",
                                        new XAttribute(XNamespace.Xmlns + "fxsp", fxsp),

                                        from myRow in
                                        dc.Elements("row").AsEnumerable().Where(x => x.GetElementValue("ProductType").Equals("A"))
                                        join shiftRow in
                                        dc.Elements("row").AsEnumerable().Where(x => x.GetElementValue("ProductType").Equals("B")) on
                                        myRow.GetElementValue("Identifier") equals shiftRow.GetElementValue("Identifier")
                                        into joined
                                        from shiftRow in joined.DefaultIfEmpty()

                                        //new { t1A = myRow.GetElementValue("Value"), t2A = shiftRow.GetElementValue("Value") }

                                        group myRow by new { ccy = myRow.GetElementValue("Ccy") }
                                        into myGroup
                                        select
                                        new XElement(fxsp + "pair",
                                            new XAttribute(xc + "value", myGroup.Key.ccy),
                                                from myRow in myGroup
                                                select
                                                new XElement(fxsp + "pair",
                                                    new XAttribute(xc + "value", myRow.GetElementValue("Identifier")),
                                                    new XAttribute(xc + "type", "Fields"),
                                                    new XElement(mp + "ask", new XAttribute(xc + "keyFormat", "N"), myRow.GetElementValue("Value")),
                                                    new XElement(mp + "bid", new XAttribute(xc + "keyFormat", "N"), myRow.GetElementValue("Value")),
                                                    new XElement(mp + "mid", new XAttribute(xc + "keyFormat", "N"), myRow.GetElementValue("Value")),
                                                    new XElement(mp + "quotation", new XAttribute(xc + "keyFormat", "C"),
                                                        myRow.GetElementValue("Identifier")))))))))));

怎么解决? 我需要在group into子句之后使用shiftRow的值,这使得shiftRow不再可访问。对不起冗长的代码...

0 个答案:

没有答案