5

I have a query in EF Core 1.1.2 that is evaluated on client side and would like to know if there is a better way to translate it into sql?

The query:

from l in _ctx.Locations
  join i in _ctx.Inventories on l.Id equals i.LocationId
  join it in _ctx.Items on i.ItemId equals it.Id
  where l.ProjectId == projectid
  group i by new {l.Id, l.LHA} into il
  select new InventoryLocations() {
      Id= il.Key.Id,
      LHA = il.Key.LHA,
      FlaggedItems = il.Any(x=>x.Item != null && x.Item.Flagged)
  }

If not, what other options do I have?

  • As I know there's still no way mapping views.
  • FromSQL() method can return types already known in the context only and I can not mark one model as [NotMapped] for example.
  • Moving back to ef6 is not an option because .net core is the target framework.

Models:

public class Location
{
    public Guid Id { get; set; }

    [ForeignKey("Project")]
    public Guid ProjectId { get; set; }
    public Project Project {get; set; }
    public string Name { get; set; }
    public string LHA { get; set; }

    [ForeignKey("ScanUser")]
    public Guid? ScanUserId { get; set; }
    public User ScanUser { get; set; }
    [ForeignKey("CheckUser")]
    public Guid? CheckUserId { get; set; }
    public User CheckUser { get; set; }

    [ForeignKey("GroupLeader")]
    public Guid? GroupLeaderId { get; set; }
    public User GroupLeader { get; set; }
    public int State { get; set; }
}

public class Inventory
{
    public Guid Id { get; set; }

    [ForeignKey("Project")]
    public Guid ProjectId { get; set; }
    public Project Project {get; set; }
    public string EANCode { get; set; }
    [ForeignKey("Location")]
    public Guid LocationId { get; set; }
    public Location Location { get; set; }
    public Double ScanQty { get; set; }
    [ForeignKey("ScanUser")]
    public Guid? ScanUserId { get; set; }
    public User ScanUser { get; set; }
    public DateTime? ScanDate { get; set; }
    [ForeignKey("Item")]
    public Guid? ItemId { get; set; }
    public Item Item { get; set; }

    [ForeignKey("InventoryTask")]
    public Guid? InventoryTaskId { get; set; }
    public InventoryTask InventoryTask { get; set; }

    [ForeignKey("CheckUser")]
    public Guid? CheckUserId { get; set; }
    public User CheckUser { get; set; }
    public DateTime? CheckDate { get; set; }
    public Double PrevQty { get; set; }
}

public class Item
{
    public Guid Id { get; set; }
    [ForeignKey("Project")]
    public Guid ProjectId { get; set; }
    public Project Project {get; set; }
    public string ItemNo { get; set; }
    public string EANCode { get; set; }
    public string Name { get; set; }
    public Double Price { get; set; }
    public bool Deleted { get; set; }
    public DateTime ChangeTime { get; set; }

    public Double BaseQty { get; set; }
    public bool Flagged { get; set; }
}
5
  • Avoid GroupBy queries if possible. Looking at the posted query, it should be doable - neither joins nor group by is needed. Hope you have navigation properties like Location.Inventories and Inventory.Item ? Can you post the entity models?
    – Ivan Stoev
    Jul 21, 2017 at 12:42
  • @IvanStoev I've included my models. Inventory.Item does exists but I don't have Location.Inventories. Could it help somehow? The application handles multiple projects so location, item and inventory are quite big datasets. (~2-3M items and ~4-5 inventories per item)
    – Perrier
    Jul 21, 2017 at 13:39
  • Your [ForeignKey] attributes are wrong. They should appear on the navigation property and reference the key field e.g. [ForeignKey("ProjectId")] public Project Project { get; set; }.
    – Brad
    Jul 22, 2017 at 7:34
  • @Brad I think it can be used this way as Guid ProjectId is a foreign key by ef naming convention. My ForeignKey attributes are just points to the right navigation property.
    – Perrier
    Jul 22, 2017 at 8:59
  • Using ProjectId etc. does conform to naming conventions and EF would be using this to determine the relationships. But your usage of the [ForeignKey] attribute is wrong and serves absolutely no purpose. I'm surprised EF doesn't throw an exception when you generate your migrations but it must be ignoring it because it's already figured out the relationships by naming convention. learn.microsoft.com/en-us/ef/core/modeling/relationships
    – Brad
    Jul 22, 2017 at 10:01

2 Answers 2

5

Currently (and looks like also in the incoming EF Core v.2.0) the GroupBy queries are processed locally, so the key is to avoid them where possible.

And your query seems to be eligible for that - there is no need to first multiply the data set with joins and then group it back.

I've noticed you use only reference navigation properties and FKs in your entities, basically like database table record and SQL. But EF allows you to define also a corresponding collection navigation properties which allow you to start queries from the logical root, thus eliminating the need of joins and group by.

If you define navigation property from Location to Inventory

public class Location
{
    // ...
    public ICollection<Inventory> Inventories { get; set; }
}

then the equivalent query could be simply:

from loc in _ctx.Locations
where loc.ProjectId == projectid
select new InventoryLocations()
{
    Id = loc.Id,
    LHA = loc.LHA,
    FlaggedItems = loc.Inventories.Any(inv => inv.Item != null && inv.Item.Flagged)
}

which will be fully translated to SQL.

If for some reason you can't create the above collection navigation property, still you can start with locations and manually correlate them with inventories:

from loc in _ctx.Locations
where loc.ProjectId == projectid
select new InventoryLocations()
{
    Id = loc.Id,
    LHA = loc.LHA,
    FlaggedItems = _ctx.Inventories.Any(inv => loc.Id == inv.LocationId && inv.Item != null && inv.Item.Flagged)
}
0

If you add the navigation property as Ivan correctly suggests:

public class Location
{
    // ...
    public ICollection<Inventory> Inventories { get; set; }
}

Then you can simply create a query like this:

var locations = _ctx.Locations
   .Include(x => x.Inventories)
       .ThenInclude(x => x.Item)
   .Where(x => x.ProjectId == projectId)
   .Select(loc => new InventoryLocations
   {
        Id = loc.Id,
        LHA = loc.LHA,
        FlaggedItems = loc.Inventories.Any(inv => inv.LocationId == loc.Id && inv.Item?.Flagged)
   });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.