Zapytanie LINQ - Ilość wpisów dla konkretnej Daty.

0

Cześć, mam klasę

 class Item
    {
         public DateTime Date { get; set; }
         public string UserName { get; set; }
         public int Count { get; set; } 
    }
```następnie kolekcje
```csharp
        List<Item> mojeObiekty = new List<Item>();

i teraz chce ją wyświetlić w ten sposób by wyświetliła się ilość wpisów UserName dla konkretnej Daty, robię to tak:

var query = from moje in mojeObiekty
                        group moje by new
                            {
                                moje.UserName,
                                moje.Data,
                               
                            } into gcs
                        select new
                        {
                            UserName = gcs.Key.UserName,
                            Data = gcs.Key.Data,
                            Count = gcs.Count(),
                        };

Ale błędnie działa, zwraca pojedyncze wiersze i count=1, co źle robie ?

0

chce ją wyświetlić w ten sposób by wyświetliła się ilość wpisów UserName dla konkretnej Daty

int liczbaObiektowSpelniajacychWarunek = mojeObiekty.Where(x => x.Date == TWOJADATA).Count();

http://ideone.com/TnMPcj

0

Nie do końca, chcę by z automatu wyświetlił listę z datą bez jej podawani, np
05.04.2017 marek 5
04.04.2017 marek 6
04.04.2017 łukasz 3
itd..

1

Chodzi o coś takiego?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Soft
{
    class Item
    {
        public string Name { get; set; }
        public DateTime Date { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var items = new List<Item>
            {
                new Item { Name = "First", Date = new DateTime(2017,04,05,0,0,0) },
                new Item { Name = "First", Date = new DateTime(2017,04,05,0,0,0) },
                new Item { Name = "Second", Date = new DateTime(2017,04,04,0,0,0) },
                new Item { Name = "Third", Date = new DateTime(2017,04,03,0,0,0) }
            };

            items.GroupBy(it => new { it.Date, it.Name })
                .Select(g => new { Name = g.Key.Name, Date = g.Key.Date, Count = g.Count() })
                .ToList()
                .ForEach(el => Console.WriteLine("Name: {0}, Date: {1}, Count: {2}", el.Name, el.Date, el.Count));
        }
    }
}

PS: Sorry ale tej sql'o - podobnej składni nie trawię :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1