如何使用自定义对象列表加入和过滤Entity Framework上下文?
public class Car
{
public int CarId;
public DateTime Start;
public DateTime End;
}
List<Car> all = new List<Car>();
//... code that fills all collection
var y = context.Car.Join(all,
eua => eua.Car.UserID,
euaP => all.Car.UserID, (e1, e2) => new { e1 });
//Filter by date
我想做类似于
的事情CREATE TABLE #Car
(
ID uniqueidentifier,
DateTimeStart datetime,
DateTimeEnd datetime
)
SELECT *
FROM dbo.Car C
INNER JOIN #Car CTemp ON C.ID = CTemp.ID
WHERE
(DATEADD(s, 1, C.DateTimeStart) between CTemp.DateTimeStart AND CTemp.DateTimeEnd)
AND (DATEADD(s,1, C.DateTimeEnd between CTemp.DateTimeStart AND CTemp.DateTimeEnd))
答案 0 :(得分:2)
您只能在内存中加入内存对象。因此,除非将内存中的对象加载到数据库中(此时它们不再位于内存中),否则无法加入数据库。但是,您可以以非常具体的方式在数据库中进行过滤。例如,如果您有要匹配的ID列表,则可以使用Contains
,如下所示:
List<Guid> ids = inMemoryCars.Select(o => o.ID).ToList();
List<Car> matchingCarsFromDB = context.Cars.Where(c => ids.Contains(c.ID)).ToList();