2010-03-17 13 views
1

나는 두 가지 질문이있다.foreach 루프

1) 나의 인터페이스는 IRegister라고 불리는 인터페이스를 가지고 있으며, IPerson이라는 또 다른 인터페이스를 가지고있다.

2) 두 개의 List (IRegister, IPerson)가 있는데 둘 다 하나 이상의 행을 가질 수 있습니다.

두 목록을 모두 루프하는 가장 좋은 방법은 무엇입니까? GetValidationRules 내에서?

public interface IRegister 
{ 
    string FirstName { get; } 
    string MiddleName { get; } 
    string LastName { get; } 
    string EmailAddress { get; }   
    List<IPerson> Student { get; } 
} 


public static List<ValidationRule> GetValidationRules(List<IRegister> register) 
    { 
     List<ValidationRule> validationRules = new List<ValidationRule>(); 



     foreach (IRegister myregister in register) 
     { 
      if (string.IsNullOrEmpty(myregister.FirstName)) 
       validationRules.Add(new ValidationRule("Reg", "Must have aFirst Name")); 
      if (string.IsNullOrEmpty(myregister.LastName)) 
       validationRules.Add(new ValidationRule("Reg", "Must have a Last Name")); 
      if (string.IsNullOrEmpty(myregister.EmailAddress)) 
       validationRules.Add(new ValidationRule("Reg", "Must have a Email Address")); 

    IPerson here? how 
     } 
+0

나는 C#에 대해 이야기하고있는 것으로 추측하고 있지만, 지정했다면 좋을 것입니다. – FrustratedWithFormsDesigner

+0

plz에 질문하기 나는 많은 사람들이 이것을 얻게 될 것이라고 생각하지 않는다. – Midhat

+0

이해하려는 노력 - 등록이되어 있고, 등록이 유효하다면, 사람을 만들고 싶습니까? –

답변

1

동안 나는 당신이 당신의 질문에 따라 ... 난 당신이 루프 내부 IRegister의 Student 때에 프로퍼티를 반복하는 방법을 궁금해하는 추측하고를 달성하기 위해 노력하고 무엇을 100 % 확인 ...

public interface IRegister 
{ 
    string FirstName { get; } 
    string MiddleName { get; } 
    string LastName { get; } 
    string EmailAddress { get; }   
    List<IPerson> Student { get; } 
} 

public static List<ValidationRule> GetValidationRules(List<IRegister> register) 
{ 
    List<ValidationRule> validationRules = new List<ValidationRule>(); 



    foreach (IRegister myregister in register) 
    { 
     if (string.IsNullOrEmpty(myregister.FirstName)) 
      validationRules.Add(new ValidationRule("Reg", "Must have aFirst Name")); 
     if (string.IsNullOrEmpty(myregister.LastName)) 
      validationRules.Add(new ValidationRule("Reg", "Must have a Last Name")); 
     if (string.IsNullOrEmpty(myregister.EmailAddress)) 
      validationRules.Add(new ValidationRule("Reg", "Must have a Email Address")); 

     foreach (IPerson person in myregister.Student) 
     { 
      // Not sure what properties you want to check for because 
      // you didn't show us what the IStudent interface looks like 
      // so I will just assume that the IStudent object has a 
      // property for EmailAddress as well 

      if (string.IsNullOrEmpty(person.EmailAddress)) 
       validationRules.Add(new ValidationRule("Reg", "Student must have a Email Address")); 
     } 
    } 
} 
+0

감사합니다. 제시카 빈 –

1

IPerson 모든 인스턴스를 반복 할 가장 쉬운 방법은 IRegister 인스턴스 내에서 IPerson의 목록을 평평하게 SelectMany를 사용하는 것입니다. 예

foreach (var person in register.SelectMany(x => x.Student)) { 
    ... 
} 

들어 이것은 IRegister 모든 값 IPerson 모든 인스턴스를 포함하는 IEnumerable<IPerson>을 생성하는 효과를 갖는다.

2

음, 중첩 루프가 마커에 표시됩니다.

foreach (IPerson peep in myregister.Student) { ... }

+0

감사 폴 .... –