2

I have a custom type that i fill with data from two tables. I've tried the following to join two tables, this normally works. But the group by and count is causing errors:

public List<sLoadingList> GetOrdersforLoadingByWard(string _mywCode, DateTime _orderDate, int _periodId)
{
    var _formattedDate = _orderDate.Date;
    List<sLoadingList> query = (from o in CateringEntities.Orders
                                join i in CateringEntities.OrderItems
                                on o.OrderId equals i.OrderId
                                where o.LocationsLookUp.WardCode.Equals(_mywCode) &&
                                DbFunctions.TruncateTime(o.Date) == _formattedDate && o.Period == _periodId
                                select new sLoadingList()
                               {
                                   ItemId = i.ItemId,
                                   ItemName = i.Item.ItemName,
                                   ItemType = i.ItemType,
                                   Quantity = i.Item.ItemName.Count()
                               }).GroupBy(i => i.ItemId).Select(i => i.FirstOrDefault()).ToList();
    return query.ToList();

This is currently giving error:

{"DbExpressionBinding requires an input expression with a collection 
                                                ResultType.\r\nParameter name: input"}

But if i remove :

Quantity = i.Item.ItemName.Count()

the query runs ok.

1 Answer 1

2

Essentially you trying to use an aggregation (the Count()) on an set of data you have not grouped yet.

Try grouping by the ItemId, ItemName and ItemType first and then selecting the section with the Count() in it.

As I commented, without seeing all the tables etc it is hard to give an exact answer but try something like this -

List<sLoadingList> query = 
(
    (
        from o in first
        join i in second 
        on o.OrderId equals i.OrderId
        where ... some condition ...
        select new sLoadingList()
        {
            ItemId = i.ItemId
            , ItemName = i.Item.ItemName
            , ItemType = i.ItemType
        }
    )
    .GroupBy(i => i.ItemId)
    .ThenBy(i => i.ItemName)
    .ThenBy(i => i.ItemType)
    .Select(i => new {KeyData = i.Key, Cnt = i.Item.ItemName.Count()})
)
.GroupBy(i => i.KeyData.ItemId)
.Select(i => i.FirstOrDefault())
.ToList();

Essentially you create a query that pulls out the data you want to summarise, then group it and then you can apply an aggregation such as count.

A couple of tips -

  • Remember you do not have to do it in one query, try splitting on smaller ones that are easier to understand
  • Try LinqPad as this is good for testing stuff like this (www.linqpad.net)
Sign up to request clarification or add additional context in comments.

4 Comments

I did try grouping using something like : group i by i.Item.ItemName into g.... But i couldnt get the joined tables into the selected object
With out knowing all the table structures it is hard to comment but I will put together an example.
ok thanks, basically i have orders table (orderid,name...) that joins to order_items (itemId, itemName....) i'm trying to group by order items on the name and add the count. But also want some info from the orders table in the select.
ThenBy is not available in the version of entity i'm using. any other alternative?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.