Grouping and summing values for food takeout
Scope

If all you need is to calculate some values from a set of data tables, it would suffice to use LINQ. However, if you prefer to provide an API with common functionality concerning orders and order details, you should really create your own domain classes and map the data tables to your domain classes. For the sake of this question, I would opt for using LINQ directly on the data tables.

Review

You are iterating your 3 data tables kassaOrders, orderDetails and menuDetails over and over again with each order detail having a link to a kassa order and each menu detail having a link to a kassa order. We can easily optimize performance and reduce code redundancy by caching these tables in lookup collections.

Lookup Tables

Start by caching the kassa orders in a dictionary by order id (called serial in the orders). This way, fast lookup is possible to join the details.


Data.kassaOrders.AsEnumerable()
    .Where(x =&gt; x.Field&lt;bool&gt;("btwticket") &amp;&amp; 
          !x.Field&lt;bool&gt;("terugname") &amp;&amp; !x.Field&lt;bool&gt;("teruggenomen"))



 var orders = Data.kassaOrders
    .AsEnumerable()
    .Where(x =&gt; 
         x.Field&lt;bool&gt;("btwticket") &amp;&amp; 
        !x.Field&lt;bool&gt;("terugname") &amp;&amp; 
        !x.Field&lt;bool&gt;("teruggenomen"))
    .ToDictionary(order =&gt; order.Field&lt;int&gt;("serial"));


We can then use lookup tables - which are dictionaries that store a sequence of values instead of a value - to cache the order and menu details by order id. This code shows how to do it for eating in, equivalent LINQ could be written for take out.


Data.orderDetails.AsEnumerable()
   .Where(d =&gt; d.Field&lt;int&gt;("orderid") == x.Field&lt;int&gt;("serial") 
       &amp;&amp; !d.Field&lt;bool&gt;("takeout"))



 var orderDetailsEatIn = Data.orderDetails
    .Where(order =&gt; !order.Field&lt;bool&gt;("takeout"))
    .ToLookup(detail =&gt; detail.Field&lt;int&gt;("orderid"));


and


Data.MenuDetails.AsEnumerable()
   .Where(d =&gt; d.Field&lt;int&gt;("orderid") == x.Field&lt;int&gt;("orderid"))



var menuDetailsEatIn = Data.MenuDetails
   .Where(order =&gt; !order.Field&lt;bool&gt;("takeout"))
   .ToLookup(detail =&gt; detail.Field&lt;int&gt;("orderid"));


Calculate Amount

You're calculating the amount using a peculiar GroupBy. There is no reasong to group by the take out variable, because you filter on a specific value right before !d.Field&lt;bool&gt;("takeout"). The call to Distinct also doesn't make sense since details are already grouped by key and keys are distinct anyway.


GroupBy(x =&gt; new { 
    takeout = x.Field&lt;bool&gt;("takeout"), 
    orderid = x.Field&lt;int&gt; ("orderid") }).Distinct().Count();



Using our lookup tables, we can easily calculate the amount as below. To stay within your spec, we count the number of orders that have at least one order detail available. Note that an empty collection is returned orderDetailsEatIn[key] is the key is not present in an ILookup.

 var EatinAmount = orders
    .Select(order =&gt; orderDetailsEatIn[order.Key])
    .Count(details =&gt; details.Any());


Calculating Total

Your spec is a bit strange to me. I would expect Order 1--&gt;* OrderDetail 1--&gt;* MenuDetail. Instead the cardinality is Order 1--&gt;* OrderDetail, Order 1--&gt;* MenuDetail with the price for each menu detail multiplied by the number of order details to calculate the subtotal by menu detail.

We could calculate the subtotal for the order details as follows:

var subtotalOrderDetailEatIn = orders
    .SelectMany(order =&gt; orderDetailsEatIn[order.Key])
    .Select(detail =&gt; detail.Field&lt;decimal&gt;("price"))
    .DefaultIfEmpty(0).Sum();


and the subtotal for menu details:

var subtotalMenuDetailEatIn = orders
    .SelectMany(order =&gt; orderDetailsEatIn[order.Key]
        .SelectMany(detail =&gt; menuDetailsEatIn[order.Key]))
    .Select(detail =&gt; detail.Field&lt;decimal&gt;("prijs"))
    .DefaultIfEmpty(0).Sum();


yielding a grant total of:

var TotalEatin = subtotalOrderDetailEatIn + subtotalMenuDetailEatIn;


Note that this is just one possible way to use LINQ to calculate these values from data tables. Other ways could include:


Create intermediate classes, value tuples or domain classes to store strongly typed properties to facilitate querying readability.
A more advanced LINQ approach using GroupJoin to build complex classes given several flattened collections and join selectors.


It's really up to you to see how comfortable you are exploring the realms of LINQ.