2009-08-26 20 views
1

行,所以在這裏我們去,我的課叫做產品之一是這樣實現的:有人請澄清此IEnumerable <T>業務爲我的MVC應用程序?

namespace DomainModel.Entities 
{ 
    public class Product 
    { 
     public int ProductID { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
     public decimal Price { get; set; } 

    } 
} 

我想補充一個IEnumerable保存值的列表,我可以通過在我看來以後迭代。我怎麼做到這一點,爲什麼MSDN示例代碼如此混亂(這根本無法幫助我,我感覺它比這更簡單)? (見下文)

using System; 
using System.Collections; 

public class Person 
{ 
    public Person(string fName, string lName) 
    { 
     this.firstName = fName; 
     this.lastName = lName; 
    } 

    public string firstName; 
    public string lastName; 
} 

public class People : IEnumerable 
{ 
    private Person[] _people; 
    public People(Person[] pArray) 
    { 
     _people = new Person[pArray.Length]; 

     for (int i = 0; i < pArray.Length; i++) 
     { 
      _people[i] = pArray[i]; 
     } 
    } 

     IEnumerator IEnumerable.GetEnumerator() 
     { 
      return new PeopleEnum(_people); 
     } 
} 

public class PeopleEnum : IEnumerator 
{ 
    public Person[] _people; 

    // Enumerators are positioned before the first element 
    // until the first MoveNext() call. 
    int position = -1; 

    public PeopleEnum(Person[] list) 
    { 
     _people = list; 
    } 

    public bool MoveNext() 
    { 
     position++; 
     return (position < _people.Length); 
    } 

    public void Reset() 
    { 
     position = -1; 
    } 

    public object Current 
    { 
     get 
     { 
      try 
      { 
       return _people[position]; 
      } 
      catch (IndexOutOfRangeException) 
      { 
       throw new InvalidOperationException(); 
      } 
     } 
    } 
} 

class App 
{ 
    static void Main() 
    { 
     Person[] peopleArray = new Person[3] 
     { 
      new Person("John", "Smith"), 
      new Person("Jim", "Johnson"), 
      new Person("Sue", "Rabon"), 
     }; 

     People peopleList = new People(peopleArray); 
     foreach (Person p in peopleList) 
      Console.WriteLine(p.firstName + " " + p.lastName); 

    } 
} 

/* This code produces output similar to the following: 
* 
* John Smith 
* Jim Johnson 
* Sue Rabon 
* 
*/ 

回答

5

哪些錯誤與List<Person>

+0

+1 - 20秒! – 2009-08-26 17:52:15

0

你應該嘗試使用一個列表,而不是你目前使用的是什麼:

List<Product> products = new List<Product>(); 
0

你不看像你將需要延長IEnumerable

嘗試是這樣的:

static void Main() 
{ 
    var peopleArray = new IEnumerable<Person> 
    { 
    new Person("John", "Smith"), 
    new Person("Jim", "Johnson"), 
    new Person("Sue", "Rabon"), 
    }; 

    foreach (var p in peopleArray) 
    { 
     Console.WriteLine(p.firstName + " " + p.lastName); 
    } 
} 
0

如果你想要做的只是添加一個人的集合(一次去過),那麼下面的確是足夠的:

namespace DomainModel.Entities 
{ 
    public class Product 
    { 
     public int ProductID { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
     public decimal Price { get; set; } 
     public IEnumerable<Person> People { get; set; } 
    } 
} 

這是有點不好意思,你必須一次性分配所有人。你可以讓它成爲一個像List一樣的實際可變集合,或者提供一個AddPerson()方法來隱藏它(處理可能會多次添加同一個人)

如果這是基於一個支持表,但我認爲使用上面的(非常)簡單的建議沒有任何數據庫後端人員和產品之間的一些外鍵關係,你應該考慮讓數據庫訪問層處理這個你...

/映射會簡單:

public Product GetExampleProduct() 
{ 
    return new Product() { 
     ProductID = 1, 
     Name = "SkeetOMatic", 
     Description = "your very own Skeet in a box", 
     Price = Double.PositiveInfinity, 
     People = new[] { new Person("Joel"), new Person("Jeff") } 
    }; 
} 

只需在數據庫中使用正確的外鍵和using the Linq designer應該讓你注意到這種關聯,讓Linq爲你編寫所有這些鍋爐板。然後,如果您詢問與此產品相關的人員,您會自動獲得他們。