English 中文(简体)
按季度分类
原标题:Linq group month by quarters
  • 时间:2011-11-11 03:11:01
  •  标签:
  • c#
  • linq

是否能够按季度、从小到小到小到小区、Ie Q1 Jan到从小到小到小到小等。

最佳回答

类似情况

Enumerable.Range(1,12)
    .Select(o => new DateTime(DateTime.Today.Year, o, 1))
    .GroupBy(o => (o.Month - 1) / 3)
问题回答

这方面的一个基本例子表明:

var dates = new[]
                    {
                        new DateTime(2011, 12, 25), 
                        new DateTime(2011, 11, 25),
                        new DateTime(2011, 5, 4),
                        new DateTime(2011, 1, 3), 
                        new DateTime(2011, 8, 9),
                        new DateTime(2011, 2, 14),
                        new DateTime(2011, 7, 4),
                        new DateTime(2011, 11, 11)
                    };

var groupedByQuarter = from date in dates
                        group date by (date.Month - 1)/3
                        into groupedDates
                        orderby groupedDates.Key
                        select groupedDates;


foreach(var quarter in groupedByQuarter)
{
    Console.WriteLine("Q: {0}, Dates: {1}", quarter.Key, string.Join(", ", quarter));
}

那里的秩序只是帮助这些地方合乎逻辑地命令他们。 在小组发言之后,你可以删除整个条款。

With the output of:

Q: 0, Dates: 1/3/2011 12:00:00 AM, 2/14/2011 12:00:00 AM
Q: 1, Dates: 5/4/2011 12:00:00 AM
Q: 2, Dates: 8/9/2011 12:00:00 AM, 7/4/2011 12:00:00 AM
Q: 3, Dates: 12/25/2011 12:00:00 AM, 11/25/2011 12:00:00 AM, 11/11/2011 12:00:00 AM

显然,你需要纠正季度数字,增加一个或也许将其转化为相应的数字。

您可以将任意名单分成若干部分,使用选用方法和组别。 在您看来,假定名单上的几个月符合正确的顺序,应当做到以下几点:

var groups = yourList
    .Select((item, index) => new { item, index })
    .GroupBy(indexedItem=>indexedItem.index/3);

Yes it can be achieved with the LINQ to SQL GroupBy function if you define the qouters somewhere in your database or you can write some code that will handle this action it all depends on what data is available to you for this evaluation.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签