LINQ to Entities - How to Convert SQL query (UnionAggregate) into LINQ

472 views Asked by At

I Would like to convert the following SQL query in its LINQ to Entity version:

select
 geometry::UnionAggregate(geometries.GeometryBounds) 
from 
( select 
    GeometryBounds
  from 
    Province where id in ( 1, 2 )
  union all
  select 
    GeometryBounds
  from 
    Region where id in ( 1, 2 )
  union all
  select 
    GeometryBounds
  from 
    Country where id in ( 1, 2 ) 
) as geometries
1

There are 1 answers

0
Nikita On

In LINQ union all functional provided by Collection.Concat() method.

var ID = new[] {1, 2};

var query = (youContext.Province
            .Select(x => x.GeometryBounds))
            .Concat
            (youContext.Region
            .Select(x => x.GeometryBounds))
            .Concat
            (youContext.Country
            .Select(x => x.GeometryBounds));