2011-03-07 68 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)); 

从这里你可以做你想做的,批量插入到另一个集合或什么的。