×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

linq drill down statement

linq drill down statement

linq drill down statement

(OP)
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

RE: linq drill down statement

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.

RE: linq drill down statement

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.

RE: linq drill down statement

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 --> C#

var query = from c in doc.Root.Descendants(W.p)
            where c.Descendants(w + "pStyle").FirstOrDefault() != null
            select c.Descendants(w + "pStyle").FirstOrDefault().Attribute(w + "val");
 
foreach (XAttribute attr in query)
    if (strStylesAll.Contains(attr.Value))
    {
        if (strStyles.Contains(attr.Value))
            strReferenceNumber++;
        strStylesname.Add(attr.Value + "p " + strReferenceNumber);
    } 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close