0

I have a method which returns the object of type CarsCache. I want to set some properties in my object Vehicle which has object of Car from CarChache object. I can do it easily like below:

var carCahe =  getByCarId(1);

Vehicle v = new Vehicle();
v.Car.value1 = carCahe.value1;

But I want to do it through LINQ. I have done something like this through LINQ but that method returns the collection and then I use where and select. I have a object Vehicle and there is an object Car in vehicle. I do it like this:

entity = new Vehicle
{

    IsActive = true,
    Car = DataCache.GetInstance()
          .GetAllCars()
          .ToList()
          .Where(o => o.carId== carId)
          .Select(o => new Car{Value1= o.value1, value2= o.value2})
          .Single()
    };

This is also working now I want to do this with method which returns only one object. Not collection but I am unable to do this. I am doing this:

entity =new Vehicle
{

  IsActive = true,
  Car= DataCache.GetInstance() // it retruns carCahe object.
         .GeTCarById(1)
         //Here i want to set value1 property and value2 property
 };

How can I do this?

Tim
  • 28,212
  • 8
  • 63
  • 76
Umer Waheed
  • 4,044
  • 7
  • 41
  • 62
  • IMO, When return value of `GeTCarById` is a car with new values your approach is correct ;). – shA.t Feb 10 '16 at 06:14
  • To be honest: If your first approach is working, why do you want to make it more complicated and less readable? The first approach is totaly clear just by seeing it once for me. The LINQ approach is much more error-prone, less debugable, less readable and actually not needed if the other one is working. – ckruczek Feb 10 '16 at 06:16
  • @ckruczek agreed. LINQ is slow but i just want to do it for my knowledge. – Umer Waheed Feb 10 '16 at 06:20

2 Answers2

3

I don't think there is a nice way to implement the second approach using LINQ, as getByCarId returns just single item, not list. LINQ methods often operate on enumerables, so you cannot use methods like Select in your case.

Why don't you get the data from cache in first statement and assign it to use it to create Vehicle in another statement:

var carCache = DataCache.GetInstance().GeTCarById(1);
var entity = new Vehicle
{
  IsActive = true,
  Car = new Car { Value1 = carCache.value1, value2 = carCache.value2 }
};
dotnetom
  • 24,551
  • 9
  • 51
  • 54
  • Thanks for reply. I done it. Can we do this in object initializer? – Umer Waheed Feb 10 '16 at 06:22
  • 1
    Both `Vehicle` and `Car` are already using object initializers. However you could simplify initializing of `Car` property as described in http://stackoverflow.com/questions/16794925/nested-object-initializer-syntax – dotnetom Feb 10 '16 at 06:28
2

Please try like this:

var carCache = DataCache.GetInstance().GeTCarById(1);
entity =new Vehicle
{
    IsActive = true,
    Car.value1 = carCache.value1,
    Car.value2 = carCache.value2
};

I assume Car property in class Vehicle has already filled Car object somehow - at least new instance.

madoxdev
  • 3,770
  • 1
  • 24
  • 39