-2
public class parent
{
    public string abc { get; set; }
    public childclass pos { get; set; }
}

public class childclass
{
    public string value { get; set; }
}

I am currently reading the collection of parent class

var obj =
(from dia in parent
    select new
    {
        abc = dia.abc,
        pos = new childclass()
        {
            value = dia.pos.value
        },
    })
.ToList();

how do I read the nested class childclass using linq to object , this piece is not working

pos = new childclass()
{
    Value = dia.pos.Value
}, 

Please advise

Servy
  • 202,030
  • 26
  • 332
  • 449
user1005310
  • 737
  • 2
  • 12
  • 40

2 Answers2

1

C# is case-sensitive. The value field in your class childClass has a lower-case 'v'. Your LINQ statement is referencing it using an upper-case 'V'.

Try the following code:

var obj = (from dia in parent
            select new
            {
                abc = dia.abc,
                pos = new childclass()
                {
                    value = dia.pos.value // 'Value' has been changed to 'value'
                },
            }).ToList();

Here is a test I ran to verify that the updated LINQ statement works:

var parents = new List<parent>()
{
    new parent{abc = "abc", pos = new childclass{ value = "value" }}
};
var obj =
            (from dia in parents // changed to 'parents' to match my variable above
            select new
            {
                abc = dia.abc,
                pos = new childclass()
                {
                    value = dia.pos.value
                },
            }).ToList();

foreach (var par in obj)
{
    Console.WriteLine(par);
}

The resulting output was:

"{ abc = abc, pos = ProgrammingTestBed.Program+childclass }"

Jon Senchyna
  • 7,867
  • 2
  • 26
  • 46
  • still not the underlying problem though, just a typo. – Servy Jun 08 '12 at 19:57
  • Well it actually compiles fine for me when I make that change. – Jon Senchyna Jun 08 '12 at 19:57
  • Well, there's also a missing semi-colon in the definition of `parent` (I've edited the OP since though). Having said that, the fundamental design of the query seems...off, but before fixing it we'd need to know what the asker is trying to do. – Servy Jun 08 '12 at 19:59
  • I am passing a list of parent , it is reading abc fine but not able to read nested class, getting error " cannot convert class childclass to string" – user1005310 Jun 08 '12 at 20:03
  • True, I updated the definition of `parent` when I was trying to compile it and forget to mention that. Good catch. – Jon Senchyna Jun 08 '12 at 20:03
1

It works perfectly, I think your problem is case sensitive

Classes:

public class Parent
{
    public string Abc { get; set; }

    public Childclass Pos { get; set; }
}

public class Childclass
{
    public string Value { get; set; }
}

Use:

var parent = new List<Parent> {                
    new Parent { Abc = "abc1", Pos = new Childclass { Value = "Value1" } },
    new Parent { Abc = "abc2", Pos = new Childclass { Value = "Value2" } }
};

var obj =(from dia in parent select new {
                abc = dia.Abc,
                pos = new Childclass()
                {
                    Value = dia.Pos.Value
                },
        }).ToList();
andres descalzo
  • 14,887
  • 13
  • 64
  • 115