I have two related tables:
OrderHeader
Id
Truck_name
Group
OrderItem
Id
OrderID
Location
Read
OrderItem contains column OrderHeaderId (foreign key relationship).
My WebService has to select specific Order with orderItems and return it(after some parsing) to client. I have to use eager loading because of important data in OrderItem.My database query needs to load first not finished (Status IN 1402,1403 AND Read=0) order. So:
context.Configuration.LazyLoadingEnabled = false;
var query = context.OrderHeader
.Where(o=> new[] { 1402, 1403 }.Contains(o.Status) && o.OrderItem.Any(g => g.Read == 0))
.OrderBy(o => o.Id)
.GroupBy(g => g.Group)
.FirstOrDefault()
.AsQueryable()
.Include("OrderItem");
I use GroupBy to take first not finished Group and every time it returns me an empty OrderItem table. Why? How to include child table after GroupBy statement?
Related
Ok, imagine that I have EF 6.1.3 and database first model (stored in edmx file) that contains an entity named Product.
That entity have 4 columns, for example: Id (int), Name {string), Code (string), Rank (int). The columns are stored in that order as I described and you can see that in a model browser.
Then i'm writing a query like this:
var list = mycontext.Products
.Where(x => x.Id < 10)
.GroupBy(x => new { x.Code, x.Name })
.Select(x=> new { x.Key.Code, x.Key.Name, MaxRank = x.Max(z => z.Rank) })
.ToList();
As you can see, I used an another columns order in my group by clause (Code column, then Name column).
But in SQL query I have such query text:
....
GROUP BY
[Extent1].[Name]
, [Extent1].[Code]
....
The columns in group by clause always ordered in same order which they have in a edmx file.
It just a sample, but that situation always break my query plans, cause sql starting to use wrong indexes and etc.
Anybody know how can I fix that?
without sort
with sort
I'll have this query in joining the table but it did not returned data from Include table or Join table.
var tasks = (from item in ctx.Tasks
join tp in ctx.TaskPlugins
on item.TaskId equals tp.TaskId
select item)
.Include(x => x.TaskPlugins).Include(x => x.TaskPlugins.Select(p => p.Plugin)).Include(x=>x.TaskPlugins.Select(p=>p.Plugin.Store));
return ctx.Tasks.ToList();
But this query does not return data from TaskPlugins
Error Message: ((System.Data.Entity.DynamicProxies.Task_6F777A6C52D9E84FD3DF53481564A61969CE62ABBA9D985448F99BFB8A49A2D7)new System.Collections.Generic.Mscorlib_CollectionDebugView<oRouter.Model.Task>(task).Items[0]).TaskPlugins
Thanks.
One thing, You should be returning tasks.ToList() and not ctx.Tasks.ToList()
Second, the last include .Include(x=>x.TaskPlugins.Select(p=>p.Plugin.Store) is the only one needed. First 2 includes are NOT needed.
Consider a (simplified) table structure like this:
[USERS]
EMPID
NAME
[APPOINTMENTS]
(FK_APPT_USER) EMPID
APPTTYPEID
COMPLETE
Each user can have 0..* appointments, each of which can be one of many APPTYPEID's, and can either be complete or not complete.
I want to filter the result set of a IQueryable[USER] query such that it only includes USERS who have an appt of some typeID (say 1) and where the COMPLETE field is in a list of values. I'm doing this as part of a gridview filter that allows users to select either to show only completed or not completed users for particular appointment types.
List<string> vals = new List<string> {"Y","N"}
//maybe the user has only selected Y so the above list only contains 1 element
var qry = ctx.USER.Where(x=> vals.Contains( ? ));
//bind etc
This is really easy to do if the values I'm comparing against the list are in a 1-1 relationship with the USER object, for example:
var qry = ctx.USER.Where(x=> vals.Contains(x.NAME));
But I don't understand how to do it with a 1-many relationship like with my appointments table, it's getting me all brain-tied trying to conceptualize the entity sql for it. Can anybody explain how to do this?
qry = ctx.USER.Where(u => u.APPOINTMENTS
.Where(a => a.APPTYPEID == 1)
.Any(a => vals.Contains(a.COMPLETE)));
UPDATE (added returning those users, which do not have appointments at all)
qry = ctx.USER.Where(u =>
!u.APPOINTMENTS.Any() ||
u.APPOINTMENTS.Any(a => a.APPTYPEID == 1 && vals.Contains(a.COMPLETE)));
I have a set of tables in MySQL - Customer 1-many Order 1-many OrderProduct many-1 Product.
I generated an EF model from the database, and other than this issue, it's working well.
If I load a given Customer, I get their Orders and OrderProducts as expected. But the Navigation property (FK) joining the OrderProduct match table to the Product table won't load.
I've actually copied the generated SQL into a MySQL query window and I do get the joined data from the Product table. But the data isn't apparently mapped into the class on return.
I've tried any number of eager/lazy loading combinations, including what's in there now:
context.orders.Where(o => o.UserID == UserID && o.OrderDate == OrderDate.Value)
.Include(o => o.orderproducts.Select(p => p.product))
.First();
But no matter what I do, product is always null.
Any suggestions?
Try this:
var order = context.orders.Include("orderproducts.product")
.Where(o => o.UserID == UserID && o.OrderDate == OrderDate.Value)
.First();
Suppose I have two tables: Category and Product. I want use linq-to-sql to select all categories (with products) that have duplicated products.
My query goes like this:
from p in db.Products
group p by p.CategoryId into c
select new
{
categoryId = c.Key,
products = from PbyC in c
group PbyC by PbyC.Name into dupl
where dupl.Count() > 1
select dupl;
}
It works but LINQPad lists empty Categories (without any duplicated Product). How to modify the query?
It would be great to have Category name display somehow instead of Id.
EDIT: I have relationship between tables.
db.Products.GroupBy(p => new { p.Name, p.CategoryId })
.Select(g => new { g.Key.CategoryId, Count = g.Count })
.Where(ng => ng.Count > 1)
.Select(ng => ng.CategoryId);
That'll select the ids of the categories with more than one Product of the same name, and is assuming you don't have any relationships set up between the objects so you'll need to join the results to the Categories table to get detailed info.
If you do have a relationship set up then you can always change the CategoryId to Category (or whatever the related object is named) and just select Category.Name in the last Select.