Nicolas92
Nicolas92

Reputation: 23

C# Entity Framework and list of objects

I'm using first time entity framework. I'm developing console application.

I have list of objects and one object contains two string variables: carName and carStatus.

I have database, which have one table, which contains ID and Name.

How I can compare this list to entity set? I want to know, if database do not contain carName, so I can add it to database.

Here is some of my code:

List<Cars> car = new List<Cars>();


// adding new objects to list.
car.Add(new Cars(carName, carStatus);


// Entity
CarEntities db = new CarEntities();


foreach (var car in db.vehicles)
{
    // print out all vehicles in database...
    Console.WriteLine(car.Name.ToString());
}

Upvotes: 0

Views: 1231

Answers (1)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101701

You can use Any method:

using(CarEntities db = new CarEntities())
{
    if(!db.vehicles.Any(v => v.Name == "carName"))
    {
       // add new car to db
    }
}

Upvotes: 1

Related Questions