2011-03-07 67 views
2

每個InfringementEntity都有一個類型。獨特使用Linq查詢

foreach (InfringementEntity ie in _infCol.InfCollection.Select(r=>r).Distinct()) 
{ 
    InfringementLodgementEntity.InfringementCollection.InfCollection.Add(ie); 
} 

InfringementLodgementCollection.InfringementLodgementEntities 
    .Add(InfringementLodgementEntity); 

,我需要選擇所有侵權實體與不同類型和新的InfringementLodgementEntity插入。然後將此侵權隱私實體添加到侵權投訴收藏中。

問題是如何選擇不同類型的侵權實體將其添加到新的侵權隱藏實體中。

回答

0

如果我理解你的問題,你可以使用OfType()

var theInfringementEntitiesYouWant = _infCol.InfCollection.OfType<TheTypeYouWant>().Distinct(); 

我離開了.Select(r=>r)因爲它沒有做任何有用的事情。

0
public abstract class BaseClass 
{ 

    private Type _classType; 
    public Type ClassType 
    { 
     get 
     { 
      return _classType; 
     } 
     set 
     { 
      _classType= value; 
     } 
    } 

    public abstract Type GetType(); 
} 

public class InheritedClass: BaseClass 
{ 

    public override Type GetType() 
    { 
     if (ClassType == null) 
     { 
      ClassType = typeof(InheritedClass);//ie SingleInfringement or DblInfringment 
     } 
     return ClassType; 
    } 
} 

我找到對付這只是有一個抽象方法GetType()在其定義必須在繼承的類中重寫基類的最簡單方法。

反射相當昂貴,應在大多數情況下謹慎使用。所以當我們使用它時,我們只是存儲反射的結果。

這就允許我們做:

var entities = _infCol.InfCollection.Where(w => w.GetType() == typeof(DesiredType)); 

從這裏你可以做你想做的,批量插入到另一個集合或什麼的。