[英]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. 我有对象列表,一个对象包含两个字符串变量:carName和carStatus。
I have database, which have one table, which contains ID and Name. 我有数据库,其中有一个表,其中包含ID和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.
我想知道,如果数据库不包含carName,那么我可以将其添加到数据库中。
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());
}
You can use Any
method: 您可以使用
Any
方法:
using(CarEntities db = new CarEntities())
{
if(!db.vehicles.Any(v => v.Name == "carName"))
{
// add new car to db
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.