Код: Выделить всё
select foo.*
, (case when hst.Id is not null and foo.OtherColumn = 1 then 42 else null end) as Bar
from FooEntities foo
left join
FooEntities for system_time all hst on hst.Id = foo.Id
and hst.OtherColumn = 999
< /code>
FooEntitiesЯ использую первый подход (объекты, генерируемые из DB), поэтому начинайте с: < /p>
Код: Выделить всё
// generated from DB
public partial class FooEntity
{
public int Id { get; set; }
public byte OtherColumn { get; set; }
... other properties...
}
public partial class MyDbContext : DbContext
{
public virtual DbSet FooEntities { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity(... mapping configuration...);
}
}
< /code>
Now I add BarКод: Выделить всё
// manually created
public partial class FooEntity
{
public int? Bar { get; set; }
}
public partial class MyDbContext
{
partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
{
modelBuilder.Entity(entity => entity.Ignore(e => e.Bar));
}
}
< /code>
I then have the following LINQ-to-entities method to encapsulate the above query:
public IQueryable QueryFoosWithBar(IQueryable foos)
{
return foos
.GroupJoin(
_dbContext.Set().TemporalAll().Where(hst => hst.OtherColumn == 999),
foo => foo.Id,
hst => hst.Id,
(foo, hst) => foo.MapBar(hst.DefaultIfEmpty().First() != default
&& foo.OtherColumn == 1 ? 42 : null));
}
< /code>
where MapBarКод: Выделить всё
public static FooEntity MapBar(
this FooEntity foo,
int? bar)
{
foo.Bar = bar;
return foo;
}
< /code>
If I materialise the result of QueryFoosWithBarThe LINQ expression ... could not be translated
In other words, this works:
Код: Выделить всё
var results = await QueryFoosWithBar(... expression...).ToArrayAsync();
< /code>
but this doesn't:
var results = await QueryFoosWithBar(... expression...).OrderBy(foo => foo.Id).ToArrayAsync();
< /code>
I presume it's because EF Core doesn't understand that the projected FooEntityКод: Выделить всё
BarПодробнее здесь: https://stackoverflow.com/questions/797 ... n-the-data
Мобильная версия