首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c#连接DefaultIfEmpty文件使用Linq DefaultIfEmpty()生成重复

c#连接DefaultIfEmpty文件使用Linq DefaultIfEmpty()生成重复
EN

Stack Overflow用户
提问于 2015-08-10 01:54:14
回答 1查看 200关注 0票数 0

我试图在两个XML文档上完成一个leftOuterJoin,但是我目前使用的方式似乎是从fileone返回一些奇怪的重复部分(在下面的代码中解释)。我试过很多种方法,但都想不出怎么做。有人能帮我解决这个问题吗?事先非常感谢!

fileone:

代码语言:javascript
复制
<fileone>

    <Book BookID="dog"> Dog </Book>

    <Book BookID="cat"> Cat </Book>

</fileone>

文件二:

代码语言:javascript
复制
<filetwo>

       <Edition BID="cat" OrderID="100"> about cat</Edition>

       <Edition BID="cat" OrderID="200">more about cat</Edition>

</RightSeq>

我要找的是(bookID=BID上的左外部连接):

代码语言:javascript
复制
<item>
   <Book BookID="cat"> Dog </Book>
</item>
<item>
    <Book BookID="cat"> Cat </Book>
    <Edition BID="cat" OrderID="100"> about cat</Edition>
</item>
<item>
    <Book BookID="cat"> Cat </Book>
    <Edition BID="cat" OrderID="200"> more cat</Edition>
</item>

我的(错误)代码:

代码语言:javascript
复制
      var result = from a in fileone.Descendants()
      join b in secondxdoc.Descendants()
     on (string)a.Attribute("BookID") equal (string)b.Attribute("BID") into inners
      from ele in inners.DefaultIfEmpty()

      select new XElement("item", new XElement(a), ele == null ? null : new XElement(ele));
        var output = new XElement("LeftOuterJoin", result);         
    }

目前的错误结果:

代码语言:javascript
复制
<item>
   <fileone>  
       <Book BookID="dog"> Dog </Book> // this entire <fileone> bit is unwanted
       <Book BookID="cat"> Cat </Book>  //I don't know what I've done wrong
   </fileone> 
</item>
    <item>
   <Book BookID="cat"> Dog </Book>
</item>
<item>
    <Book BookID="cat"> Cat </Book>
    <Edition BID="cat" OrderID="100"> about cat</Edition>
</item>
<item>
    <Book BookID="cat"> Cat </Book>
    <Edition BID="cat" OrderID="200"> more cat</Edition>
</item>  
EN

回答 1

Stack Overflow用户

发布于 2015-08-10 03:05:35

我没有使用传统的linq就得到了结果。在文件2中没有匹配项的情况下,联接不会给出结果。

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input1 =
               "<fileone>" +
                   "<Book BookID=\"dog\"> Dog </Book>" +
                   "<Book BookID=\"cat\"> Cat </Book>" +
               "</fileone>";
            XDocument fileone = XDocument.Parse(input1);

            string input2 =
               "<filetwo>" +
                      "<Edition BID=\"cat\" OrderID=\"100\"> about cat</Edition>" +
                      "<Edition BID=\"cat\" OrderID=\"200\">more about cat</Edition>" +
               "</filetwo>";
            XDocument secondxdoc = XDocument.Parse(input2);

            //<item>
            //   <Book BookID="cat"> Dog </Book>
            //</item>
            //<item>
            //    <Book BookID="cat"> Cat </Book>
            //    <Edition BID="cat" OrderID="100"> about cat</Edition>
            //</item>
            //<item>
            //    <Book BookID="cat"> Cat </Book>
            //    <Edition BID="cat" OrderID="200"> more cat</Edition>
            //</item>
            XElement output = new XElement("Root");
            foreach (XElement item in fileone.Descendants("Book"))
            {
                string bookId = item.Attribute("BookID").Value;
                List<XElement> bookTwoItems = secondxdoc.Descendants("Edition").Where(x => x.Attribute("BID").Value == bookId).ToList();
                if (bookTwoItems.Count  == 0)
                {
                    XElement newItem = new XElement("item");
                    output.Add(newItem);
                    newItem.Add(item);
                }
                else
                {
                    foreach (XElement bookTwoItem in bookTwoItems)
                    {
                        XElement newItem = new XElement("item");
                        output.Add(newItem);
                        newItem.Add(item);
                        newItem.Add(bookTwoItem);
                    }
                }
                
            }
        
        }
    }
}
​

让它与左外接一起工作

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input1 =
               "<fileone>" +
                   "<Book BookID=\"dog\"> Dog </Book>" +
                   "<Book BookID=\"cat\"> Cat </Book>" +
               "</fileone>";
            XDocument fileone = XDocument.Parse(input1);

            string input2 =
               "<filetwo>" +
                      "<Edition BID=\"cat\" OrderID=\"100\"> about cat</Edition>" +
                      "<Edition BID=\"cat\" OrderID=\"200\">more about cat</Edition>" +
               "</filetwo>";
            XDocument secondxdoc = XDocument.Parse(input2);

            //<item>
            //   <Book BookID="cat"> Dog </Book>
            //</item>
            //<item>
            //    <Book BookID="cat"> Cat </Book>
            //    <Edition BID="cat" OrderID="100"> about cat</Edition>
            //</item>
            //<item>
            //    <Book BookID="cat"> Cat </Book>
            //    <Edition BID="cat" OrderID="200"> more cat</Edition>
            //</item>
            var results = (from one in fileone.Descendants("Book")
                           join two in secondxdoc.Descendants("Edition") on one.Attribute("BookID").Value equals two.Attribute("BID").Value into inners
                           from two in inners.DefaultIfEmpty()
                           select new { fileone = one, filetwo = two == null ? null : two }).ToList(); 
            XElement output = new XElement("Root");
            foreach (var item in results)
            {
                XElement newItem = new XElement("item");
                output.Add(newItem);
                newItem.Add(item.fileone);
                if (item.filetwo  != null)
                {
                        newItem.Add(item.filetwo);
                }

            }

        }
    }
}
​

或者这种方法

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input1 =
               "<fileone>" +
                   "<Book BookID=\"dog\"> Dog </Book>" +
                   "<Book BookID=\"cat\"> Cat </Book>" +
               "</fileone>";
            XDocument fileone = XDocument.Parse(input1);

            string input2 =
               "<filetwo>" +
                      "<Edition BID=\"cat\" OrderID=\"100\"> about cat</Edition>" +
                      "<Edition BID=\"cat\" OrderID=\"200\">more about cat</Edition>" +
               "</filetwo>";
            XDocument secondxdoc = XDocument.Parse(input2);

            //<item>
            //   <Book BookID="cat"> Dog </Book>
            //</item>
            //<item>
            //    <Book BookID="cat"> Cat </Book>
            //    <Edition BID="cat" OrderID="100"> about cat</Edition>
            //</item>
            //<item>
            //    <Book BookID="cat"> Cat </Book>
            //    <Edition BID="cat" OrderID="200"> more cat</Edition>
            //</item>
            XElement output = new XElement("Root");
            var results = (from one in fileone.Descendants("Book")
                           join two in secondxdoc.Descendants("Edition") on one.Attribute("BookID").Value equals two.Attribute("BID").Value into inners
                           from two in inners.DefaultIfEmpty()
                           select new { fileone = one, filetwo = two == null ? null : two })
                           .Select(x => x.filetwo == null ? new XElement("item", x.fileone) : new XElement("item", new object[] { x.fileone, x.filetwo }));
            foreach (var item in results)
            {
                output.Add(item);
            }
        }
    }
}
​
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31910630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档