Populating objects in C#
I have Order,OrderDetails and OrderStatus objects as shown below:
public class Order
{
public override int OrderId { get; set; }
public string FName { get; set; }
public string MName { get; set; }
public string LName { get; set; }
public string Street { get; set; }
public string City { get; set; }
public List<OrderDetails> OrderDetails { get; set; };
}
public class OrderDetails
{
public override int OrderdetailsId { get; set; }
public int OrderId { get; set; }
public int ProductID { get; set; }
public int Qty { get; set; }
public List<OrderStatus> OrderStat { get; set; };
}
public class OrderStatus
{
public override int OrderdetailsStatusId { get; set; }
public int OrderdetailsId { get; set; }
public int StatusID { get; set; }
}
I cannot use LinQ. I want to populate the order object like we do in LinQ.
How do I populate all all the properties in Order object: for eg.
Order o =new Order();
o.FName="John";开发者_运维问答
o.LName="abc";
o.Street="TStreet";
o.City="Atlanta";
then o.Orderdetails.Add(orderdetails)
How do I do that here in C# when not using LinQ.
Are you talking about being able to do this:
Order o =new Order
{
FName="John",
LName="abc",
Street="TStreet",
City="Atlanta"
};
Because you sould still be able to do that, even without LINQ;
You could even go so far as to:
Order o =new Order
{
FName="John",
LName="abc",
Street="TStreet",
City="Atlanta"
};
o.OrderDetails = new List<OrderDetail>();
o.Add(new OrderDetail { OrderdetailsId = 1, Qty = 0 /* etc */});
But that can get a bit confusing.
If you're referring to this syntax:
Order o = new Order()
{
FName = "John",
LName = "abc",
Street = "TStreet",
City = "Atlanta"
};
It has nothing to do with LINQ. You can still use object initializer syntax when you're targeting .NET 2.0, as long as you use a C# 3.0 compiler. The compiler will generate exactly the same IL as it would for your original code.
Basically what you have looks fine. You can use delegates and events, as well, if you need more control from the class.
If you are asking how you would populate from a data store, sounds like you are desiring to create an object graph. You can declare the class as serializable, look that up.
精彩评论