EDN Admin
Well-known member
In general when I know the fields Ill get back from an linq query I write a function like the one below, which works nicely.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public IEnumerable MatchPats
{
<span style="color:Blue; get
{
<span style="color:Blue; var data = <span style="color:Blue; from matchpats <span style="color:Blue; in <span style="color:Blue; this._docIndividualPats.Root.Element(<span style="color:#A31515; "ARGUMENTS").Element(<span style="color:#A31515; "ENTRY").Descendants(<span style="color:#A31515; "PATTERN")
<span style="color:Blue; select <span style="color:Blue; new
{
Action = matchpats.Value.Substring(0,1),
Pattern = matchpats.Value.Substring(1,3),
};
<span style="color:Blue; return data.ToList();
}
}
[/code]
What I need to do now is query an xml and I dont know how many fields I need to create. The value I get back from the query is one string but several positions in the string have a different meaning. Like in the example above, the first position indicate
the action, the 2nd to 5th position indicate an id, and then there is an unknown amount of fields. How can I translate that properly to a list? I tried something like the query below but that doesnt work, as I need to specify a max amount of fields and
the null still results in an empty column. Any ideas?
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public IEnumerable MatchPats
{
<span style="color:Blue; get
{
<span style="color:Blue; var data = <span style="color:Blue; from matchpats <span style="color:Blue; in <span style="color:Blue; this._docIndividualPats.Root.Element(<span style="color:#A31515; "ARGUMENTS").Element(<span style="color:#A31515; "ENTRY").Descendants(<span style="color:#A31515; "PATTERN")
<span style="color:Blue; select <span style="color:Blue; new
{
Action = matchpats.Value.Substring(0,1),
Pattern = matchpats.Value.Substring(1,3),
Field1 = (matchpats.Value.Length - 4 >= 1 ? matchpats.Value.Substring(4, 1) : <span style="color:Blue; null),
Field2 = (matchpats.Value.Length - 4 >= 2 ? matchpats.Value.Substring(5, 1) : <span style="color:Blue; null),
Field3 = (matchpats.Value.Length - 4 >= 3 ? matchpats.Value.Substring(6, 1) : <span style="color:Blue; null),
Field4 = (matchpats.Value.Length - 4 >= 4 ? matchpats.Value.Substring(7, 1) : <span style="color:Blue; null),
Field5 = (matchpats.Value.Length - 4 >= 5 ? matchpats.Value.Substring(8, 1) : <span style="color:Blue; null),
Field6 = (matchpats.Value.Length - 4 >= 6 ? matchpats.Value.Substring(9, 1) : <span style="color:Blue; null),
Field7 = (matchpats.Value.Length - 4 >= 7 ? matchpats.Value.Substring(10, 1) : <span style="color:Blue; null),
Field8 = (matchpats.Value.Length - 4 >= 8 ? matchpats.Value.Substring(11, 1) : <span style="color:Blue; null),
Field9 = (matchpats.Value.Length - 4 >= 9 ? matchpats.Value.Substring(12, 1) : <span style="color:Blue; null),
};
<span style="color:Blue; return data.ToList();
}
}
[/code]
View the full article
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public IEnumerable MatchPats
{
<span style="color:Blue; get
{
<span style="color:Blue; var data = <span style="color:Blue; from matchpats <span style="color:Blue; in <span style="color:Blue; this._docIndividualPats.Root.Element(<span style="color:#A31515; "ARGUMENTS").Element(<span style="color:#A31515; "ENTRY").Descendants(<span style="color:#A31515; "PATTERN")
<span style="color:Blue; select <span style="color:Blue; new
{
Action = matchpats.Value.Substring(0,1),
Pattern = matchpats.Value.Substring(1,3),
};
<span style="color:Blue; return data.ToList();
}
}
[/code]
What I need to do now is query an xml and I dont know how many fields I need to create. The value I get back from the query is one string but several positions in the string have a different meaning. Like in the example above, the first position indicate
the action, the 2nd to 5th position indicate an id, and then there is an unknown amount of fields. How can I translate that properly to a list? I tried something like the query below but that doesnt work, as I need to specify a max amount of fields and
the null still results in an empty column. Any ideas?
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public IEnumerable MatchPats
{
<span style="color:Blue; get
{
<span style="color:Blue; var data = <span style="color:Blue; from matchpats <span style="color:Blue; in <span style="color:Blue; this._docIndividualPats.Root.Element(<span style="color:#A31515; "ARGUMENTS").Element(<span style="color:#A31515; "ENTRY").Descendants(<span style="color:#A31515; "PATTERN")
<span style="color:Blue; select <span style="color:Blue; new
{
Action = matchpats.Value.Substring(0,1),
Pattern = matchpats.Value.Substring(1,3),
Field1 = (matchpats.Value.Length - 4 >= 1 ? matchpats.Value.Substring(4, 1) : <span style="color:Blue; null),
Field2 = (matchpats.Value.Length - 4 >= 2 ? matchpats.Value.Substring(5, 1) : <span style="color:Blue; null),
Field3 = (matchpats.Value.Length - 4 >= 3 ? matchpats.Value.Substring(6, 1) : <span style="color:Blue; null),
Field4 = (matchpats.Value.Length - 4 >= 4 ? matchpats.Value.Substring(7, 1) : <span style="color:Blue; null),
Field5 = (matchpats.Value.Length - 4 >= 5 ? matchpats.Value.Substring(8, 1) : <span style="color:Blue; null),
Field6 = (matchpats.Value.Length - 4 >= 6 ? matchpats.Value.Substring(9, 1) : <span style="color:Blue; null),
Field7 = (matchpats.Value.Length - 4 >= 7 ? matchpats.Value.Substring(10, 1) : <span style="color:Blue; null),
Field8 = (matchpats.Value.Length - 4 >= 8 ? matchpats.Value.Substring(11, 1) : <span style="color:Blue; null),
Field9 = (matchpats.Value.Length - 4 >= 9 ? matchpats.Value.Substring(12, 1) : <span style="color:Blue; null),
};
<span style="color:Blue; return data.ToList();
}
}
[/code]
View the full article