-2

Suppose there are two properties in Myclass: Date, Symbol I want to frequently convert between those two properties, but I find that for List <Myclass> vector if I use

vector.groupby(o => o.Date).Select(o => o)

the vector is no longer the type of List<IGrouping<string, Myclass>>

And if I want to convert groupby(o => o.Date) to groupby(o => o.Symbol) I have to use

vector.groupby(o => o.Date).Selectmany(o => o).groupby(o => o.Symbol)

I try to use SortedList<Date, Myclass>, but I am not familiar with SortedList(actually, I don't know what's the difference between SortedList and Groupby). Is there any effective way to achieve such effect, as I highly depend on the speed of running?

int volDay = 100;
Datetime today = new DateTime(2012, 1, 1);

//choose the effective database used today, that is the symbol with data more than volDay
var todayData = dataBase.Where(o => o.Date <= today).OrderByDescending(o => o.Date)
                .GroupBy(o => o.Symbol).Select(o => o.Take(volDay))
                .Where(o => o.Count() == volDay).SelectMany(o => o);

//Select symbols we want today
var symbolList = todayData
                .Where(o => o.Date == today && o.Eqy_Dvd_Yld_12M > 0))
                .OrderByDescending(o => o.CUR_MKT_CAP)
                .Take((int)(1.5 * volDay)).Where(o => o.Close > o.DMA10)
                .OrderBy(o => o.AnnualizedVolatility10)
                .Take(volDay).Select(o => o.Symbol).ToList();

//Select the database again only for the symbols in symbolList
var portfolios = todayData.GroupBy(o => o.Symbol)
               .Where(o=>symbolList.Contains(o.Key)).ToList();

This is my real code, dataBase is the total data, and I will run the cycle day by day(here just given a fixed day). The last List portfolios is the final goal I want obtain, you can ignore other properties, which are used for the selections under the collection of Date and Symbol

A.Oreo
  • 301
  • 3
  • 16

1 Answers1

0

It may be faster, or at least easier to read, if you performed a .Distinct().

To get distinct Dates:

var distinctDates = vector.Select(o => o.Date).Distinct()

To get distinct Symbols:

var distinctSymbols = vector.Select(o => o.Symbol).Distinct()

I asked what you were trying to accomplish so that I can provide you with a useful answer. Do you need both values together? E.g., the unique set of symbols and dates? You should only need a single group by statement depending on what you are ultimately trying to achieve.

E.g., this question Group By Multiple Columns would be relevant if you want to group by multiple properties and track the two unique pieces of data. a .Distinct() after the grouping should still work.

Community
  • 1
  • 1
ps2goat
  • 8,067
  • 1
  • 35
  • 68