Вопрос по c#, linq – Linq выберите новый объект

28

У меня есть запрос linq

var x = (from t in types select t).GroupBy(g =>g.Type)

который группирует объекты по их типу, в результате я хочу иметь один новый объект, содержащий все сгруппированные объекты и их количество. Что-то вроде этого:

type1, 30    
type2, 43    
type3, 72

чтобы быть более понятным: результаты группировки должны быть в одном объекте, а не объект для каждого типа элемента

Ваш Ответ

6   ответов
1

Error: User Rate Limit Exceeded

var types = new[] {typeof(string), typeof(string), typeof(int)};
var x = types
        .GroupBy(type => type)
        .ToDictionary(g => g.Key, g => g.Count());
foreach (var kvp in x) {
    Console.WriteLine("Type {0}, Count {1}", kvp.Key, kvp.Value);
}
Console.WriteLine("string has a count of {0}", x[typeof(string)]);
21

Error: User Rate Limit Exceeded

List<ObjectType> objectList = similarTypeList.Select(o =>
    new ObjectType
    {
        PropertyOne = o.PropertyOne,
        PropertyTwo = o.PropertyTwo,
        PropertyThree = o.PropertyThree
    }).ToList();
Error: User Rate Limit Exceeded
3
var x = from t in types
        group t by t.Type into grouped
        select new { type = grouped.Key,
                     count = grouped.Count() };
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
25

Error: User Rate Limit ExceededobjectsError: User Rate Limit ExceededtypesError: User Rate Limit Exceeded

var query = types.GroupBy(t => t.Type)
                 .Select(g => new { Type = g.Key, Count = g.Count() });

foreach (var result in query)
{
    Console.WriteLine("{0}, {1}", result.Type, result.Count);
}

Error: User Rate Limit ExceededwantError: User Rate Limit Exceeded

var query = types.GroupBy(t => t.Type)
                 .ToDictionary(g => g.Key, g => g.Count());

Error: User Rate Limit ExceededthenError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded Jeff
Error: User Rate Limit Exceeded Jeff
Error: User Rate Limit Exceeded Jeff
Error: User Rate Limit ExceededareError: User Rate Limit ExceededqueryError: User Rate Limit ExceededgotError: User Rate Limit ExceededqueryError: User Rate Limit ExceededgiveError: User Rate Limit Exceeded
Error: User Rate Limit ExceededdoError: User Rate Limit Exceeded
33

Error: User Rate Limit Exceeded101 LINQ SamplesError: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

var x = from t in types  group t by t.Type
         into grp    
         select new { type = grp.key, count = grp.Count() };

Error: User Rate Limit Exceeded

    // fordictionary 
  var x = (from t in types  group t by t.Type
     into grp    
     select new { type = grp.key, count = grp.Count() })
   .ToDictionary( t => t.type, t => t.count); 

   //for stringbuilder not sure for this 
  var x = from t in types  group t by t.Type
         into grp    
         select new { type = grp.key, count = grp.Count() };
  StringBuilder MyStringBuilder = new StringBuilder();

  foreach (var res in x)
  {
       //: is separator between to object
       MyStringBuilder.Append(result.Type +" , "+ result.Count + " : ");
  }
  Console.WriteLine(MyStringBuilder.ToString());   
Error: User Rate Limit Exceeded Jeff
0

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

var query2 = from c in doc.Descendants("SuggestionItem")
                select new SuggestionItem
                       { Phrase = c.Element("Phrase").Value
                         Blocked = bool.Parse(c.Element("Blocked").Value),
                         SeenCount = int.Parse(c.Element("SeenCount").Value)
                       };

Error: User Rate Limit Exceeded

var query2 = from c in doc.Descendants("SuggestionItem")
                         select new SuggestionItem(c);

Error: User Rate Limit Exceeded

List<SuggestionItem> retList = new List<SuggestionItem>();

var query = from c in doc.Descendants("SuggestionItem") select c;

foreach (XElement item in query)
{
    SuggestionItem anItem = new SuggestionItem(item);
    retList.Add(anItem);
}

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Похожие вопросы