Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple values for single key

Tags:

c#

.net

list

linq

I have a list that contains Categories and their accept/reject counts but there is a problem with this list. I'm using a LINQ query to access the data, and I grouped them by both category name and Accept/Reject Code(ResultCode). So the data is in this form:

enter image description here

Almost all of the Categories have both AP counts and RJ counts. And what I'm trying to do is to show each Category's accept and reject count. What should I use? Hashtables don't fit in this problem, I tried Dictionary with int List as value but couldn't add when the same key appeared.

UPDATE:

List<ModReportingDM.ReportObjects.AllCategories> allcats = new List<ModReportingDM.ReportObjects.AllCategories>();
Dictionary<string, ModReportingDM.ReportObjects.ResultCode> dict = new Dictionary<string, ModReportingDM.ReportObjects.ResultCode>();
ModReportingDM.ReportObjects.ResultCode x = new ModReportingDM.ReportObjects.ResultCode();
allcats = reportBLL.GetAllCats(model.ModId, model.ReportStartDate, model.ReportEndDate);
        if (allcats != null)
        {
            model.AllCatsList = new List<ModReportingDM.ReportObjects.AllCategories>();

            foreach (var item in allcats)
            {
                x.Accepted = item.Count;
                x.Rejected = item.Count;
                dict.Add(item.Category, x);

            }
        }

Query:

public List<AllCategories> GetAllCats(int modId, DateTime startDate, DateTime endDate)
    {
        using (entities = new ModReportingEntities())
        {
            var query = (from c in entities.Content
                         where c.ModId == modId && c.CreatedTime >= startDate && c.CreatedTime <= endDate && c.Category != null
                         group c by new { c.Category, c.ResultCode } into g
                         orderby g.Count() ascending
                         select new AllCategories
                         {
                             Category = g.Key.Category,
                             ResultCode = g.Key.ResultCode,
                             AcceptCount = g.Count(),
                             RejectCount = g.Count()
                         });

            return query.ToList();
        }
    }
like image 361
Burak Karakuş Avatar asked Dec 05 '25 09:12

Burak Karakuş


1 Answers

What i would do is create a ResultCode class:

public class ResultCode
{
    public int Ap { get; set; }
    public int Rj { get; set; }
}

and then use a Dictionary<string, ResultCode> which maps each category to its report.

You could also take a different approach using a Tuple<T1, T2> (which personally i like less) which simply maps your key to two distinct values:

Dictionary<string, Tuple<int, int>> categoryToResultCode;
like image 72
Yuval Itzchakov Avatar answered Dec 07 '25 22:12

Yuval Itzchakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!