tags:

views:

110

answers:

2

I'm having a heck of a time with transforming a simple SQL Query into a LINQ query(using vb btw)

Here is my SQL:

SELECT     USRDEFND5
FROM         int_gp_employee
GROUP BY USRDEFND5

The xml looks like this:

<int_gp_employee>
  <row>
    ....
    <usrdefnd5>Some GUID</usrdefnd5>
  </row>
</int_gp_employee>

I've tried a number of different variations of the LINQ. My current statement is:

From b In xmlFile...<row> Group b...<usrdefnd5> By b...<usrdefnd5> INTO group

when I foreach through the resulting collection, EVERY line (17000) shows up.

Thanks for taking a look.

+3  A: 

I'm afraid I don't know the VB equivalent for sure, but in C# this would be:

var query = from row in xmlFile.Descendants("row")
            group row by (string) row.Element("usrdefnd5");

Without using XML literals, the VB would be:

Dim query = From row In document.Descendants("row") _
            Group row By CStr(row.Element("usrdefnd5"))

EDIT: If you just need the distinct keys, then something like:

Dim query = From row In document.Descendants("row") _
            Select CStr(row.Element("usrdefnd5")) _
            Distinct
Jon Skeet
Still returns all of them. I'm looking for distinct values of usrdefnd5.
spuppett
It should be returning all the rows, but grouped by that field. Each group will be a sequence of rows with that key. Do you need the rows at all, or *just* the keys?
Jon Skeet
I just need the keys.
spuppett
I should say, I just need the DISTINCT keys.
spuppett
I don't know why this doesn't work:[code]Dim xmlFile As XDocument = XDocument.Load("h:\test\xml\int_gp_employee.xml")dim branch_ids = From row IN xmlFile...<row> SELECT row...<USRDEFND5> Distinctfor each b in branch_ids console.writeline(b.value)next [/code]Sorry, I don't know how to mark up the comments section yet.
spuppett
I'll edit my answer
Jon Skeet
A: 

Found the answer in another thread:

Here

From b In xmlFile...<row> Group b...<usrdefnd5> By b...<usrdefnd5> INTO group

should have been

From row IN xmlFile...<row> SELECT row.<USRDEFND5>.value distinct

That gets the unique value and just that column.

spuppett