Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

c# - Join 2 tables to retrieve name and count linq Entity Framework

I have 2 tables Orders and Items and I am trying to query these 2 tables for statistics retrieval.

  • Orders has columns OrderID[PK], ItemID[FK], OrderStatus etc.
  • Items has columns ItemID[PK], ItemName, ItemPrice etc.

I am fetching list of orders based on date range and then I am returning their counts based on their status.

Below is my StatisticsResponse.cs to return the response.

public class StatisticsResponse
{
    public int CancelledOrderCount { get; set; }
    public int CompletedOrderCount { get; set; }
    public int InProgressOrderCount { get; set; }
    public int TotalOrders { get; set; }
    public Dictionary<string,int> ItemOrders { get; set;}
}

This is how I am retrieving Orders between 2 dates.

var orders = _unitOfWork.OrderRepository
                        .GetMany(x => (x.OrderStatus == "Pending"
                                      && x.OrderDate.Value.Date >= dtStartDate 
                                      && x.OrderDate.Value.Date < dtEndDate) || 
                                      ((x.OrderStatus == "Completed" || x.OrderStatus == "Cancelled") 
                                        && x.DeliveryDate.Date >= dtStartDate || x.DeliveryDate.Date < dtEndDate) || (x.LastUpdated.Value.Date >= dtStartDate || x.LastUpdated.Value.Date < dtEndDate)).ToList();

if (orders != null)
{
    return new StatisticsResponse()
    {
          TotalOrders = orders.Count(),
          CancelledOrderCount = orders.Where(x => x.OrderStatus == "Cancelled").Count(),
          CompletedOrderCount = orders.Where(x => x.OrderStatus == "Completed").Count(),
          InProgressOrderCount = orders.Where(x => x.OrderStatus != "Completed" && x.OrderStatus != "Cancelled").Count()
    }
}

Now, in the ItemOrders property, which is of type Dictionary<string,int>, I want to group each item with their name and count. I have ItemID in my orders list, and I would like to join 2 tables to get the name before storing.

I have tried to use GroupBy as below but am totally stuck on how to get the name for the Item after grouping

ItemOrders = new Dictionary<string, int>
{
    orders.GroupBy(x=>x.ItemID)// Stuck here
}

I also read about GroupJoin but couldn't quite make sure whether it can fit in here.

Could someone please let me know how I can join these 2 tables to get their name based on their ID?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use something along this:

using System.Entity.Data; //to use Include()

...

Dictionary<string,int> itemOrders = dbContext.Orders.Include(o=> o.Item)
                        .GroupBy(o=> o.Item)
                        .ToDictionary(g => g.Key.Name, g => g.Count());

This is assuming:

  • There is a navigation property set up from Order to Item
  • Each Order has one Item

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...