2014-05-14 3 views
0

DevExpress XAF를 사용하고 있습니다. 목록보기가 있습니다. 목록보기에서 사용자는 여러 행을 선택할 수 있습니다. 특정 열/속성의 모든 셀이 동일한 값을 유지하는지 확인해야합니다. 그렇지 않으면 예외를 throw해야합니다.선택한 각 셀의 값이 같은지 확인하려면 어떻게합니까?

예를 들어 사용자가 3 개의 행을 선택하면 Trucker 열의 3 개의 값이 동일한 지 확인해야합니다. 여기에 지금까지 내 코드입니다 :

if (lr.PurchaseLoad.Trucker != null) 
{ 
    if (lr.PurchaseLoad.Trucker.Name == lr.PurchaseLoad.Trucker.Name == true)//Is this correct? 
    { 

      // What am I comparing here? 

    } 
    else if (lr.PurchaseLoad.Trucker.Name.Equals(lr.PurchaseLoad.Trucker.Name) == false) 
    { 
    throw new UserFriendlyException("Please make sure your selected records have the same Trucker assigned.");      
    } 


    } 

답변

1

것은이 같은 시도는 LINQ를 사용하여, (lr 가정하면 ListView입니다) :

var distinctNames = lr.SelectedItems.Cast<PurchaseLoad.Trucker>() 
            .Where(x => x != null) 
            .Select(x => x.Name) 
            .Distinct() 
            .Count(); 

if (distinctNames == 0) 
    throw new UserFriendlyException("Make at least one selection."); 
else if (distinctNames > 1) 
    throw new UserFriendlyException("Please make sure your selected records have the same Trucker assigned."); 

나는 DevExpress 컨트롤을 사용하지 않았습니다.

... = lr.SelectedObjects.Cast<PurchaseLoad.Trucker>()... 

원본 코드에서 구문이 꺼져 있습니다.

if (value1 == value2) 

if (value1 != value2) 

하지 않음 : (EB의 코멘트에서)

if (value1 == value2 == true) 

if (value1 == value2 == false) 

최종 작업 솔루션 :

foreach (LoadRelationship trucker in View.SelectedObjects) 
{ 
    var distinctNames = 
     View.SelectedObjects.Cast<LoadRelationship>() 
          .Where(x => x != null) 
          .Select(x => x.PurchaseLoad.Trucker.Name) 
          .Distinct() 
          .Count(); 

    if (distinctNames > 1) 
     throw new UserFriendlyException(
      "Please make sure your assigned Truckers are the same."); 
} 
+0

감사 그랜트 다음을 사용하여 두 값을 비교한다. 이것은 효과가 있었다. 나는 약간의 변경을했다. 그러나 나는 원하는 결과를 얻었다. 도와 줘서 고마워! 다음은 작업 코드입니다 :'foreach (View.SelectedObjects의 LoadRelationship 트럭 운전자) { var distinctNames = View.SelectedObjects.Cast () .Where (x => x! = null) .Select (x = > x.PurchaseLoad.Trucker.Name) .Distinct() .Count(); if (distinctNames> 1) throw new UserFriendlyException ("할당 된 Truckers가 동일한 지 확인하십시오."); }' –

+0

도움이 되니 기쁩니다! 당신의 답을 제게 덧붙여서 읽기 쉬울 것입니다. 주석은 코드를 형식화하지 않습니다. –

관련 문제