0

I'm trying to convert this VB.Net LINQ to C# LINQ.

Basically, what the end solution is trying to achieve is to to take in an XML file; see snippet:

<BasicFee>
        <TrialType>Trial</TrialType>
        <A>1326.85575</A>
        <B>992.409</B>
        <C>668.67075</C>
        <D>1260.50925</D>
        <E>318.8955</E>
        <F>323.30925</F>
        <G>323.30925</G>
        <H>323.44125</H>
        <I>323.169</I>
        <J>1326.85575</J>
        <K>932.877</K>
</BasicFee>

And by passing parameters, "Trial" and "B", the result would give me this value "992.409" (result from Trial / B).


EDIT - This VB is not the correct syntax to achieve the result. Please see the accepted answer.

The VB equivalent is apparently something like this;

Dim sResult As String = (From oRecord In oXML.Descendants("BasicFee") Where oRecord.< Name >.Value = "Trial").FirstOrDefault.< B >.Value

I have tried tons of different ways and I keep getting the same result (either the Trial element OR the A element values (not being able to use them both).

I was hoping there would be something similar to this:

var example = root.Elements("BasicFee").Elements().Where((c=>c.Value == "Trial" && c.Value == "A"));

Any ideas?

Thanks.

Hexie
  • 3,955
  • 6
  • 32
  • 55
  • 1
    Your VB version wouldn't result in anything, there is no `` element. – Jeff Mercado Jan 07 '16 at 06:41
  • @JeffMercado Thanks, I have since noticed that (i'll edit the question) - after Jon showed me the correct example, it all makes sense now. – Hexie Jan 07 '16 at 06:49

2 Answers2

2

As noted in comments, your VB example wouldn't work either, but it's pretty simple to do in both languages. You need to distinguish between names and values, and exactly how your filtering works:

var example = root.Elements("BasicFee")
                  .Where(x => (string) x.Element("TrialType") == "Trial")
                  .Select(x => (string) x.Element("B"))
                  .FirstOrDefault();

Or with C# 6 you could use:

var example = root.Elements("BasicFee")
                  .FirstOrDefault(x => (string) x.Element("TrialType") == "Trial")
                  ?.Element("B")?.Value;

The value will be null if there's no such element (either no matching BasicFee or no B element within it).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Still waiting to accept... After a few hours of trying and you make it look so easy! Thank you! – Hexie Jan 07 '16 at 06:46
  • Any chance you feel like going the extra mile and giving the same result for VB (for curiosity)? – Hexie Jan 07 '16 at 06:55
  • @Hexie: I don't know what the most idiomatic VB form would be, but a straight translation of the first form into VB would work fine. (Not sure whether the latest VB has the null conditional operator.) – Jon Skeet Jan 07 '16 at 07:06
0

To find the BasicFee that has that trial type and B value, in C#:

var trialType = "Trial";
var propertyName = "B";
var query = oXML.Descendants("BasicFee")
    .Where(bf => (string)bf.Element("TrialType") == trialType)
    .Select(bf => (string)bf.Element(propertyName))
    .SingleOrDefault();

The VB version on the other hand, could be written like so:

Dim trialType = "Trial"
Dim propertyName = "B"
Dim query =
    (From bf In oXML...<BasicFee> ''// ... equivalent to Descendants
    Where bf.<TrialType>.Value = trialType
    Select bf.Element(propertyName).Value).SingleOrDefault
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272