LinQ to XML - 防止将重复元素添加到xml文件中 [英] LinQ to XML - prevent duplicate elements from being added to xml file

查看:102
本文介绍了LinQ to XML - 防止将重复元素添加到xml文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我遇到了一个需要一些建议的问题。我正在尝试使用LinQ来创建XML文档。



我在使用LinQ之前使用information_schema.columns表来创建我的数据表。一列包含数据库中的表名,第二列包含与该特定表一起使用的列。



这是我到目前为止所拥有的: br />

Hi everyone!

I am running into an issue that I need some advice on. I am trying to use LinQ to create an XML document.

I am using the information_schema.columns table to create my datatable before using LinQ. One column consists of the table names in the database and the 2nd column consist of the columns that go with that particular table.

Here is what I have so far:

XDocument document = new XDocument(
     new XDeclaration("1.0", "utf-8", "yes"),
     new XElement("testmenu",
     new XElement("showwebhelp", "True"),
     new XElement("menuitem",
     new XElement("caption", yearComboBox.Text + " " + modelComboBox.Text + " Waveforms"),
														
     from distinctItem in linqCollection                            
     select new XElement("menuitem",
     new XElement("caption", distinctItem["table_name"])
					                                
		     )
	     )
     )
);
document.Save("C:\\test.xml");



我得到的输出是:


My output that I''m getting is:

<testmenu>
  <showwebhelp>True</showwebhelp>
  <menuitem>
    <caption>Vehicle Info</caption>
    <menuitem>
      <caption>Table 1</caption>
    </menuitem>
    <menuitem>
      <caption>Table 1</caption>
    </menuitem>
    <menuitem>
      <caption>Table 1</caption>
    </menuitem>
    <menuitem>
      <caption>Table 2</caption>
    </menuitem>
    <menuitem>
      <caption>Table 3</caption>
    </menuitem>
    <menuitem>
      <caption>Table 4</caption>
    </menuitem>
    <menuitem>
      <caption>Table 4</caption>
    </menuitem>
    <menuitem>
      <caption>Table 4</caption>
    </menuitem>
    <menuitem>
      <caption>Table 4</caption>
    </menuitem>
    <menuitem>
      <caption>Table 4</caption>
    </menuitem>
    <menuitem>
      <caption>Table 4</caption>
    </menuitem>
    <menuitem>
      <caption>Table 4</caption>
    </menuitem>
    <menuitem>
      <caption>Table 5</caption>
    </menuitem>
    <menuitem>
      <caption>Table 5</caption>
    </menuitem>
    <menuitem>
      <caption>Table 5</caption>
    </menuitem>
    <menuitem>
      <caption>Table 5</caption>
    </menuitem>
    <menuitem>
      <caption>Table 6</caption>
    </menuitem>
    <menuitem>
      <caption>Table 7</caption>
    </menuitem>
    <menuitem>
      <caption>Table 8</caption>
    </menuitem>
  </menuitem>
</testmenu>



我应该看到的是每个表名中的1个。如何消除重复?我是否需要创建一个具有不同表名列表的单独列表?



这就是我获取table_names的方式。




What I should be seeing is 1 of each table name. How can I eliminate the duplicates? Do I need to create a separate list that has a distinct list of table names?

This is how I''m obtaining the table_names.

DataTable linqDT = new DataTable();







SqlCeCommand tableColumnCommand = new SqlCeCommand("SELECT table_name, column_name FROM information_schema.columns " + "WHERE (table_name <> 'accountmanagement') AND (table_name <> 'docfiles') AND (table_name <> 'engineandtransmissioncodes') " + "AND (table_name <> 'enginecodemaster') AND (table_name <> 'transmissioncodemaster') AND (table_name <> 'vehicles') " + "AND (table_name <> 'waveform_files') AND (column_name <> 'vehicle_key')", conn);
            tableColumnCommand.CommandType = CommandType.Text;
            tableColumnCommand.ExecuteNonQuery();

            SqlCeDataAdapter tableColumnCommand_da = new SqlCeDataAdapter(tableColumnCommand);
            tableColumnCommand_da.Fill(linqDT);

            var linqCollection = (from item in linqDT.AsEnumerable()
                              select item["table_name"]);

推荐答案

看起来你可以使用Linq的.Distinct()方法做你想做的事:

It looks like your could do what you want with the .Distinct() method of Linq:
var linqCollection = (from item in linqDT.AsEnumerable()
                  select item["table_name"]).Distinct();


这篇关于LinQ to XML - 防止将重复元素添加到xml文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆