Re: Selecting a variable value with Linq

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi Peter,

Let me start again...

I have two queries. Both are querying from XML.

Here is a sample of my code...

var xq = from post in xd.Descendants("form")

select new

{
FormName = (string)post.Attribute("name"),
Fields =
from field in post.Elements("field")
select new
{
Type = (string)field.Attribute("type"),
// EXAMPLE, Type = "text"
Name = (string)field.Attribute("name"),
// EXAMPLE, Name = "email"
Value = (string)field
// EXAMPLE, Value = "me@xxxxxxxxxxx"
}

};



var xmap = from dmap in map.Descendants("application")
select new
{
ApplicationName =
(string)dmap.Attribute("id"),

Mappings = from mapping in
dmap.Elements("mapping")
select new
{
XMLField =
(string)mapping.Attribute("XMLField")
}
};


System.Text.StringBuilder fieldValue = new
System.Text.StringBuilder();


foreach (var mapItem in xmap.First().Mappings)
{

if (mapItem.XMLField.Contains(":"))
{
string[] xField = mapItem.XMLField.Split(':');
fieldValue.Append("'" + xq.Select(d =>
xField[1]) + "'");
}
else
{
fieldValue.Append("'" +
xq.First().Fields.Where(a => a.Name == mapItem.XMLField).First() + "'");
}
}


In my foreach loop, I am trying to append to fieldValue. The value in xField
will be something like... form:email, (this is from the xmap query). Now,
What I need to do is to get the value held in the Value field from the xq
query. Basically, the xq query holds all the values. The xmap query defines
where in the xq query to get my value. So, for example... I need to get the
value " me@myemailcom " from the row which has the Name = "email". The text
in Name is what I am putting into the fieldValue.Append.

Imagine like the SQL below...


Col1 = "id";
Col2 = "compname";
MyVal = "dave";

Sql = "select " + Col1 + ", " + Col2 + " from table where " + Col2 + " = '"
+ MyVal + "'"; // (ignore sql injection holes, I would not normally write
like this.)

so, when I come to read it...

Label1.Text = ds.Tables(0).Field(Col2).Value;

(I may have got the syntax slightly wrong)

It is a similar effect that I need to do. Get the column by using a
variable.

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:op.urp67wrp8jd0ej@xxxxxxxxxxxxxxxxxxxx
On Wed, 01 Apr 2009 13:03:51 -0700, David
<david.colliver.NEWS@xxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hi all,

I have a query that is return items...

var q = from s in dc.mytable
select new
{
myType = (string)field
myVal = (string)Val
};

So, I am looping through something else... the loop operator could be for
the field or Val...

Here is what I am trying to achieve...

string selector = "Val";

Label1.Text = q.Select(selector);

Basically, what I am trying to do is to have a variable, selector, then
use
that to determine which column I want out of Linq query.

How do I do this.

As Pavel hints at, your question is very vague. So it's not entirely
clear what an appropriate answer would be.

However, I can tell you that variables are "captured" in a lambda used as
an anonymous method (which is how the "new { myType = (string)field; myVal
= (string)Val; }" is interpreted). Which means you can embed a variable
in your lambda, and then use it to control the behavior of the lambda
after the fact.

So, for example, you could do something like this (making some broad
guesses as to the data types you're actually using here):

string strSelect = null;
var query = from row in dc.mytable
select new
{
myType = strSelect;
myVal = row.Columns[strSelect].ToString();
};

strSelect = "column 1";
foreach (var item in query)
{
// do something with item.myType and item.myVal
}

strSelect = "column 2";
foreach (var item in query)
{
// do something with item.myType and item.myVal
}

Each time you enumerate the "query" reference, it evaluates the lambda
using the current value of "strSelect", so by changing it between the
enumerations, you get different results.

Mind you, this pattern is potentially confusing to those reading the
code. You may find it makes more sense to encapsulate the query in its
own method, and pass a specific value as an argument to be used within the
query, returning the resulting query. The downside to that would be that
you would have to declare a named type as the enumeration type and return
that, or use the non-generic IEnumerable and reflection to get the actual
properties out of the returned enumerated objects.

If that doesn't help answer your question, you should consider writing
your question in a more specific, more detailed way, including a
concise-but-complete code sample that clearly illustrates exactly what
you're trying to do.

Pete


.



Relevant Pages

  • Re: Trouble combining arrays in a table
    ... I'm querying Salesforce.com via their AJAX toolkit and outputting query ... var Account, primaryContact, lastRow; ... var table, thead, row, cell, cellText; ...
    (comp.lang.javascript)
  • Re: Selecting a variable value with Linq
    ... I have a query that is return items... ... I can tell you that variables are "captured" in a lambda used as ... var query = from row in dc.mytable ... Each time you enumerate the "query" reference, ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: EF: selecting all the relations of a relation
    ... Why do you think the direct query clumsy and inefficient? ... Let's go back to the question how to perform such a query, ... foreach (var p in people) ... foreach ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Run-time error for Invalid brackeing....
    ... Klatuu wrote: ... "var" wrote: ... may be that aspect will effect the entire Query in selecting the dates ... do i need to declare this in Query Parameters or Form text field ...
    (microsoft.public.access.forms)
  • Re: c# v3.0: implicit variable declaration, why?
    ... | variables with the 'var' type? ... The main reason for implicit variables is to allow for things like Linq ... strongly typed results into variables declared in the query. ... This example will select the single string property ContactName where the ...
    (microsoft.public.dotnet.languages.csharp)