Hi folks,
I’m struggling with what I thought would be a relatively simple process. In my last thread (http://www.daniweb.com/software-development/csharp/threads/379458), I got your help with the placement of parentheses in a Linq to XML query. I thought it was all going to be easy from there.
Now I am trying to use a method to build the query string; but cannot find a way to get the returned string to execute as a query; or find a way to use a method to build a proper query. This hard-coded query works and I get the expected output:

var contList = criteria;
        (from c in contDoc.Descendants("Content")
         where (string)c.Attribute("Type") == (str)
         from d in c.Descendants("Video")
         where (string)d.Attribute("Codec") == ("H264")
         from f in c.Descendants("Audio")
         where (string)f.Attribute("Codec") == ("Mpeg1L1")
         select (string)c.Attribute("Id"));

But, when the same query is built via a method and returned as a string, it is visually and seemingly syntactically correct; but I cannot find how to get it to execute (note: the XDocument being passed is for future use):

.....
        criteria = buildLiveCriteria(profDoc);
        var contList = criteria;
.....
}

    private static string buildLiveCriteria(XDocument docIn)
    {
      string practContent = "Content";
      string practType = "Type";
      string practVideo = "Video";
      string practCodec = "Codec";
      string practCodecVal = "H264";
      string practAudio = "Audio";
      string practAudioCod = "Mpeg1L1";
      string practId = "Id";
      string practLastLine = "select (string)c.Attribute(\"" + practId + "\"));";
      string retVal = "";

      retVal += ("(from c in contDoc.Decendants (\"" + practContent + "\")");
      retVal += ("where (string)c.Attribute(\"" + practType + "\") == (\"Live\")");
      retVal += ("from d in c.Descendants(\"" + practVideo + "\")");
      retVal += ("where (string) d.Attribute(\"" + practCodec + "\") == (\"" + practCodecVal + "\")");
      retVal += ("from f in c.Descendants(\"" + practAudio + "\")");
      retVal += ("where (string) f.Attribute(\"" + practCodec + "\") == (\"" + practAudioCod + "\")");
      retVal += (practLastLine);
      return retVal;
    }

When the query string is returned and the 'var contList = criteria' line is executed, all it get in 'contList' is the query itself. Now, I can logically see how that is happening; but can someone point me to how to properly use a method to build a query like I am trying to do?

Thanks for any help!

Thanks for the suggestion Momerath; I did see that. What I am not getting out of it, and may not have made clear in my post, is how - or if even - I can build a query with a variable number of "from" and "where" clauses. The query may need to include one to five different elements with one or more attributes to qualify against for each.

Ideas?
Thanks!

This article has been dead for over six months. Start a new discussion instead.