2013-08-06 3 views
0

У меня есть список. Мне нужно найти уникальные записи ExistingData, применяя Group By. Работает следующий код.Преобразование типа объекта во время группы.

var distinctItemsWorking = myCostPages 
     .GroupBy(x => new { 
          x.CostPageContent.Program, 
          x.CostPageContent.Group, 
          x.CostPageContent.Sequence }) 
     .Select(y => y.First()); 

Теперь мне нужно преобразовать уникальный список в список. Как мы можем достичь этого преобразования, когда мы будем группировать?

C# Метод

public List<CostPage> GetCostPages(SearchEntity search, int pageIndex, int pageSize) 
    { 
     List<ExistingData> AllData = GetExistingData(); 

     var allMatchingValues = from existingDatas in AllData 
           where existingDatas.CostPageContent.Program == search.Program 
           select existingDatas; 


     var query = allMatchingValues; 
     List<ExistingData> currentSelectionForExistingData = query 
      .Skip(pageIndex * pageSize) 
      .Take(pageSize) 
      .ToList(); 


     //var distinctItems = currentSelectionForExistingData.GroupBy(x => new { x.CostPageContent.Program, x.CostPageContent.Group, x.CostPageContent.Sequence }) 
     //     .Select(y => new CostPage() 
     //     { 
     //      CostPageContent = y.CostPageContent 
     //     } 
     //     ); 

     var distinctItemsWorking = currentSelectionForExistingData.GroupBy(x => new { x.CostPageContent.Program, x.CostPageContent.Group, x.CostPageContent.Sequence }) 
          .Select(y => y.First()); 

     List<CostPage> myCostPages = new List<CostPage>(); 
     foreach (ExistingData exist in distinctItemsWorking) 
     { 
      CostPage c = new CostPage(); 
      c.CostPageContent = exist.CostPageContent; 
      myCostPages.Add(c); 
     } 

     return myCostPages; 
    } 

Другие классы

public class ExistingData 
{ 
    public CostPageNumberContent CostPageContent { get; set; } 
    public string ItemID { get; set; } 
} 

public class CostPage 
{ 
    public CostPageNumberContent CostPageContent { get; set; } 
} 


public class CostPageNumberContent 
{ 
    public string Program { get; set; } 
    public string Group { get; set; } 
    public string Sequence { get; set; } 
} 

public class SearchEntity 
{ 
    public string Program { get; set; } 
    public string Sequence { get; set; } 
    public string ItemID { get; set; } 
} 
+0

Непонятно, чего вы пытаетесь достичь. Вы пытаетесь заменить «foreach»? –

+0

@ DanielHilgarth Я хочу удалить foreach. Также преобразование должно происходить в той же строке, что и группа By – Lijo

ответ

2

Если вы пытаетесь заменить foreach, вы можете сделать что-то вроде этого:

var myCostPages = currentSelectionForExistingData 
    .GroupBy(x => new { x.CostPageContent.Program, x.CostPageContent.Group, 
         x.CostPageContent.Sequence }) 
    .Select(y => new CostPage { CostPageContent = y.First().CostPageContent }) 
    .ToList(); 

Создание объектов CostPage в GroupBy не имеет смысла. Select - это правильное место для выполнения этого преобразования.

Смежные вопросы