Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-CE C# LINQ Forms , +
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>
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"]);
Posted 28-May-13 4:52am
Edited 28-May-13 9:09am
v5
Comments
Matt T Heffron at 28-May-13 13:55pm
   
I think you'll need to show how you get the linqCollection as that is probably where the trouble lies. Then we can offer more assistance.
joshrduncan2012 at 28-May-13 14:05pm
   
Thanks Matt! I've updated the question to reflect it.
Matt T Heffron at 28-May-13 14:35pm
   
If it actually works, please "accept" the solution.
(I meant to put this reply on the comment below...)

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
  Permalink  
Comments
joshrduncan2012 at 28-May-13 14:33pm
   
Thanks Matt!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 505
1 Maciej Los 325
2 Richard MacCutchan 265
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 28 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100