2014-09-19 69 views
1

让我们假设我有这个类的一个实例。C#在foreach循环中继续处理对象的实例

public class MyClass 
{ 
    public string LocationCode; 
    public string PickUpCode 
} 

还有另外一个类,需要一个List<MyClass>作为输入并保存到数据库。

现在我需要办理一些业务规则:

例如,如果LocationCodenullList<MyClass>这个项目必须跳过,foreach循环必须continue到列表中的下一个项目。

我已经写了下面的代码,并与nullLocationCode的项目确实是跳过,但因此当循环达到一个有效的项目,并继续将其保存在数据库中var instance = new SomeClass();莫名其妙地保留在内存中,也保存所有以前跳过的实例var instance = new SomeClass();。这意味着我在数据库中有空条目。

我正在使用NHibernateEvict没有缝隙做的伎俩。有什么建议么?

public void Save(List<MyClass> listOfItems) 
{ 
    using (UnitOfWork.Start()) 
    { 
     var repository = new Repository(); 

     try 
     { 

      foreach (var item in listOfItems.Select(i => i.Item).Where(item => item != null)) 
      { 
       var instance = new SomeClass();   

       if (pickUpCode != null) 
       { 
        instance.PickUpCode = pickUpCode; 
       } 
       else 
       {    
        instance.PickUpCode = null; 
       } 

       if (locationCode != null) 
       { 
        instance.StartLocation = locationCode 
       } 
       else 
       { 
        UnitOfWork.CurrentSession.Evict(instance); 
        continue; 
       } 

       repository.SaveSomeClass(instance); 
      } 
     } 
     catch (Exception ex) 
     { 
      _log.Error(" Unhandled error", ex); 
     } 
    } 
} 

**因为有人问,这里的一对UnitOfWork.Start()

public static class UnitOfWork 
    {   
     public static IUnitOfWork Start(); 
    } 

public interface IUnitOfWork : IDisposable 
    { 
     bool IsInActiveTransaction { get; } 
     IUnitOfWorkFactory SessionFactory { get; } 

     IGenericTransaction BeginTransaction(); 
     IGenericTransaction BeginTransaction(IsolationLevel isolationLevel); 
     void Flush(); 
     void TransactionalFlush(); 
     void TransactionalFlush(IsolationLevel isolationLevel); 
    } 
+0

你没有错过'如果检查添加到您LINQ(!item.pickUpCode = NULL)'和' if(item.locationCode!= null)'? – Michael 2014-09-19 07:26:00

+0

这是一个示例代码。实际的更详细。 – 2014-09-19 07:36:05

回答

2

为什么你不先失败并避免所有这些?

的例子是:

foreach (var item in listOfItems.Select(i => i.Item).Where(item => item != null)) 
     { 

      if (item.LocationCode == null){ 
       continue; 
      } 

      var instance = new SomeClass();   

      if (pickUpCode != null) 
      { 
       instance.PickUpCode = pickUpCode; 
      } 
      else 
      {    
       instance.PickUpCode = null; 
      } 

      // if we reach here, location code is definitley not null, no need for the check    
      instance.StartLocation = locationCode 



      repository.SaveSomeClass(instance); 
     } 

或者,你可以在where子句

foreach (var item in listOfItems.where(item=> item != null && item.LocationCode != null) 
+1

与'UnitOfWork.CurrentSession.Clear();'命令的结合起到了诀窍的作用。非常感谢你! – 2014-09-19 09:16:16

+0

非常欢迎,我喜欢尽我所能帮忙 – 2014-09-19 09:54:16

0

一些代码,而在UnitofWork.Start是如何工作的它很难提出更多的代码。但是,值得尝试在SomeClass上实现IDisposable。

+0

我也这么想过。 – 2014-09-19 07:27:53

+0

对不起您的评论偏离。迈克尔摩尔的建议是正确的,但你将无法编译。无论如何,是的,c#GC很好。我的预感是UnitOfWork.CurrentSession.Evict(instance);这个代码,不知道这个黑盒子里究竟发生了什么,它仍然可以保存实例的引用。 – meetkichu 2014-09-19 07:36:06