Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

linq drill down statement

Status
Not open for further replies.

deejayAr

Technical User
Mar 20, 2008
126
US
How can I write this to linq

first I'm doing
XElement root = doc.MainDocumentPart.GetXDocument().Root;
then
var query = from c in root.Descendants(W.p) select c;
then
foreach (XElement p in query)
{
XElement pElement = p.Descendants(w + "pStyle").FirstOrDefault() ;
if (pElement != null)
{
string spElement = pElement.Attributes(w + "val").FirstOrDefault().Value as string;
if (strStylesAll.Contains(spElement))
{
if (strStyles.Contains(spElement))
{
strReferenceNumber = strReferenceNumber + 1;
}
else
{ strReferenceNumber = strReferenceNumber; }

strStylesname.Add(spElement + "p" + " " + strReferenceNumber);
// addtoTable(spElement, strReferenceNumber)

}
}}

I know it looks ugly and not
any help would be nice
 
Please be a little clearer.
You say "How can I write this to linq " - but you are already using linq in this code!
Please tell what exactly your code is doing and what it isn't doing and what it is supposed to do.

Cheers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Also, post all of the code, and put it in code tags. It is a whole hell of a lot harder to figure out what you are doing or want to do when the code is missing information and hard to read.
 
The code is very hard to read and you have missing pieces I had to guess at, but when I indent your code properly it looks like this:

Code:
XElement root = doc.MainDocumentPart.GetXDocument().Root;

var query = from c in root.Descendants(W.p) select c;

foreach (XElement p in query)
{
	XElement pElement = p.Descendants(w + "pStyle").FirstOrDefault() ;
	if (pElement != null)
	{
		string spElement = pElement.Attributes(w + "val").FirstOrDefault().Value as string;
		if (strStylesAll.Contains(spElement))
		{
			if (strStyles.Contains(spElement))
			{
				strReferenceNumber = strReferenceNumber + 1;
			}
			else
			{
				strReferenceNumber = strReferenceNumber;
			}

			strStylesname.Add(spElement + "p" + " " + strReferenceNumber);
			// addtoTable(spElement, strReferenceNumber)

		}
	}
}

Its really hard to be sure given there are missing pieces about what you are doing, but this 'should' do the same thing.. I think...

Code:
[COLOR=#0000FF]var[/color] query = [COLOR=#0000FF]from[/color] c [COLOR=#0000FF]in[/color] doc.Root.Descendants([COLOR=#2B91AF]W[/color].p)
            [COLOR=#0000FF]where[/color] c.Descendants(w + [COLOR=#A31515]"pStyle"[/color]).FirstOrDefault() != [COLOR=#0000FF]null[/color]
            [COLOR=#0000FF]select[/color] c.Descendants(w + [COLOR=#A31515]"pStyle"[/color]).FirstOrDefault().Attribute(w + [COLOR=#A31515]"val"[/color]);
 
[COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]XAttribute[/color] attr [COLOR=#0000FF]in[/color] query)
    [COLOR=#0000FF]if[/color] (strStylesAll.Contains(attr.Value))
    {
        [COLOR=#0000FF]if[/color] (strStyles.Contains(attr.Value))
            strReferenceNumber++;
        strStylesname.Add(attr.Value + [COLOR=#A31515]"p "[/color] + strReferenceNumber);
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top