0

I have two different class and there is a relation between them. Car_Model class refers Car class with Car_ID.

To map these two class object to each other, I created relation named class. How can I assign car and modelCar into this class without making iteration? Can Linq solve this problem?

public class Car
{
    public int ID;
    public String name;
}


public class Car_Model
{
    public int ID;
    public int Car_ID;
    public String date;

}

public class relation
{
    public Car car;
    public Car_Model carModel;
}

I did not use entity framework.

List<Car> carList = new List<Car>();
List<Car_Model> carModelList = new List<Car_Model>();

Car car = new Car();
car.ID = 1;
car.name = "blabla";
carList.add(car);


Car_Model carModel = new Car_Model();
carModel.ID = 1;
carModel.Car_ID = 1;
carModel.date = "01.01.2013";
carModelList.add(carModel );
4

1 回答 1

3

最简单的方法是使用Join

var query = carList.Join(carModelList,
            car_x => car_x.Id,
            car_model => car_model.Id,
            (person, pet) => new  relation { car = car_x, carModel = car_model });

你可以在这里找到更多关于它和他的重载。

于 2013-09-10T20:27:00.903 回答