2012-05-24 20 views
13

ho i seguenti oggetti di business:Filtrare un elenco da un altro elenco C#

public class ItemCategoryBO 
    { 
     public string ItemCategory { get; set; } 
     public string Title { get; set; } 
    } 

    public class ItemBO 
    { 
     public int ItemId { get; set; } 
     public string Title { get; set; } 
     public string ItemCategory { get; set; } 
    } 

    List<ItemCategoryBO> categoryList = new List<ItemCategoryBO>(); 

    ItemCategoryBO itemCategory = new ItemCategoryBO(); 
    itemCategory.ItemCategoryCd = "CARS"; 
    itemCategory.Title = "Cars"; 

    ItemCategoryBO itemCategory2 = new ItemCategoryBO(); 
    itemCategory.ItemCategoryCd = "PLANES"; 
    itemCategory.Title = "Planes"; 

    categoryList.Add(itemCategory); 
    categoryList.Add(itemCategory2); 

    List<ItemBO> itemList = new List<ItemBO>(); 

    ItemBO item1 = new ItemBO(); 
    item1.ItemId = 1; 
    item1.Title = "1st item"; 
    item1.ItemCategoryCd = "OTHER"; 

    ItemBO item2 = new ItemBO(); 
    item2.ItemId = 2; 
    item2.Title = "2nd Item"; 
    item2.ItemCategoryCd = "CARS"; 

    ItemBO item3 = new ItemBO(); 
    item3.ItemId = 3; 
    item3.Title = "3rd Item"; 
    item3.ItemCategoryCd = "PLANES"; 

    itemList.Add(item1); 
    itemList.Add(item2); 
    itemList.Add(item3); 

Se ho un elenco di alcune categorie, come potrei trovare un elenco di elementi che contengono una categoria nella lista dei categorie? (Nel mio esempio, voglio tornare articoli 2 e 3)

+0

Quali sono le vostre liste? E per sicurezza, cosa significa "filtro per" significa qui? –

+1

Davvero non capisco i voti bassi qui. Non è molto difficile capire cosa l'OP sta chiedendo. E se davvero è abbastanza brutto da giustificare un voto negativo, lasciare all'Op dei feedback per spiegare perché li hai votati. –

+0

@CharlieKilian davvero non capisco? dopo aver commentato 'Non è molto difficile capire che cosa sta chiedendo l'OP'? –

risposta

48

Se si dispone di una situazione come:

List<ItemBO> items; 
List<ItemCategoryBO> categories; 

e si desidera ottenere tutti gli articoli che hanno una categoria che è nel tuo elenco di categorie, è possibile utilizzare questo:

List<ItemBO> result = items.Where(item => 
    categories.Any(category => category.ItemCategory.equals(item.ItemCategory))); 

L'Qualsiasi operatore enumera la sequenza sorgente e restituisce true se un qualsiasi elemento soddisfa il test proposta dal predicato. In questo caso, restituisce true se l'elenco delle categorie contiene un ItemCategoryBO in cui la sua stringa ItemCategory è uguale alla stringa ItemCategory dell'articolo. Ulteriori informazioni su di esso su MSDN

+0

Grazie mille Diana, ancora un po 'nuova per linq. Esattamente quello che stavo cercando! – stillsmallvoice

+0

Il modo semplice per spiegarlo, grazie !!!! :) – Yaz

+0

Ha funzionato anche per me –

1

provare a utilizzare un po 'di LINQ

List<ItemBO> itm = new List<ItemBO>; 
//Fill itm with data 

//get selected item from control 

string selectedcategory = cboCatetories.SelectedItem; 

var itms = from BO in itm where itm.ItemCategory = selectedcategory        select itm; 

itms now contains all items in that category 
+0

Grazie Micah, ho aggiornato la mia domanda, sto cercando un modo per filtrare dinamicamente in base a un altro elenco, non solo alla categoria di un articolo – stillsmallvoice

2

Prova questo:

List<ItemBO> items = ...; 
ItemCategoryBO category = ...; 

List<ItemBO> filteredItems = items 
    .Where(i => i.ItemCategory.Equals(category)) 
    .FirstOrDefault(); 

aggiornato per risolvere domanda aggiornato di OP:

Se ho un elenco di alcune categorie, come posso trovare un elenco di articoli che contengono una categoria nell'elenco delle categorie? (Nel mio esempio, voglio recuperare gli articoli 2 e 3)

Penso che in realtà dovresti farlo in due passaggi. Innanzitutto, ottieni il tuo elenco distinto di articoli. Quindi, dai tuoi articoli, ottieni il tuo elenco di categorie. Quindi:

// First, get the distinct list of items 
List<ItemBO> items = new List<ItemBO>(); 
foreach (var category in categories) 
{ 
    foreach (var item in category.Items) 
    { 
     if (!items.Contains(item)) 
      items.Add(item); 
    } 
} 

// Second, get the list of items that have the category. 
List<ItemBO> filteredItems = items 
    .Where(i => i.ItemCategory.Equals(category)) 
    .FirstOrDefault(); 
+0

Come posso fare questo per un elenco di categorie? – stillsmallvoice

+0

Un downvote? Nizza, L.B. –

+0

@CharlieKilian annullerò il mio downVote APPENA POSSO capisco che risposta risponda –

1

Ecco qualcosa che ho fatto in Linqpad

 
void Main() 
{ 

    var cat1 = new ItemCategoryBO {ItemCategory="c1", Title = "c1"}; 
    var cat2 = new ItemCategoryBO {ItemCategory="c2", Title = "c2"}; 

    var item1 = new ItemBO { ItemId = 1, Title = "item1", ItemCategory="c1"}; 
    var item2 = new ItemBO { ItemId = 1, Title = "item2", ItemCategory="c2"}; 
    var item3 = new ItemBO { ItemId = 1, Title = "item3", ItemCategory="c2"}; 
    var item4 = new ItemBO { ItemId = 1, Title = "item4", ItemCategory="c3"}; 

    var items = new List() {item1, item2, item3, item4}; 
    var categories = new List() {cat1, cat2}; 

    var itemsInCategory = from item in items 
    join category in categories on item.ItemCategory equals category.ItemCategory into itemInCategory 
    from categoryItem in itemInCategory 
    select new {item.Title, item.ItemCategory}; 

    itemsInCategory.Dump(); 
} 

// Define other methods and classes here 
     public class ItemCategoryBO 
     { 
      public string ItemCategory { get; set; } 
      public string Title { get; set; } 
     } 

     public class ItemBO 
     { 
      public int ItemId { get; set; } 
      public string Title { get; set; } 
      public string ItemCategory { get; set; } 
     } 

Ciò restituisce:

 
Title, ItemCategory 
item1 c1 
item2 c2 
item3 c2 
Problemi correlati