-1

I have something like this:

class SomeClass 
{
     public int Id {get;set}
     public int someProperty1 {get;set;}
     public int someProperty2 {get;set;}
     public string someProperty3 {get;set;}
     public List<SomeObject> SomeObjects {get;set}
}

When I want to get properties of this class, I do:

var db = new SomeClass();
var myClass = db.SomeClass.Single(class => class.Id == 1);

And it works perfectly fine, I can get all the properties, but the List<SomeObject> property is always null. How can I get a single myClass object, that does have all the properties, AND a list of SomeObjects? I know that I can do it like that:

var myList = db.SomeClass.SomeObjects.Single(...);

But I do not want to do it that way, is there any other way of getting WHOLE object at once?

D.Rosado
  • 5,634
  • 3
  • 36
  • 56
ojek
  • 9,680
  • 21
  • 71
  • 110

2 Answers2

1

If you are using Entity Framework, you can include a related table using the Include method. See the documentation here.

var db = new Context();
var myClass = db.SomeClass.Include("SomeObject").Single(class => class.Id == 1);
Maarten
  • 22,527
  • 3
  • 47
  • 68
0

you are making a new object for SomeClass Have you assigned values to its properties?

like

var db = new SomeClass();
db.Id=1;
db.someProperty1 = 1;
db.someProperty2 = 2;
db.someProperty3 = "abc";
db.SomeObjects = objListOfSomeObject //list reference of SomeObject

Or anywhere else you have defined these ?

and how can you call db.SomeClass ?

Jayant Varshney
  • 1,765
  • 1
  • 25
  • 42