在Linq中有6个RemoveRange

时间:2014-05-17 07:14:02

标签: linq entity-framework

IQueryable<PortServicesTariffDetail> detalis = _context.PortServicesTariffDetails
                                   .Where(w => w.PortServicesTariffId == TariffId &&
                                          w.PortServicesTariffDetailId == DetailId);
if (detalis.Count() > 0)
{
 var mydetial = detalis.GroupBy(g => new { g.StartRate,g.EndRate }).AsQueryable();
 _context.PortServicesTariffDetails.RemoveRange(mydetial.AsQueryable());
 _context.SaveChanges();
}

取决于开始和结束费率删除范围,我提出了一些问题,如匿名类型。那我该怎么办?

  

参数1:无法从'System.Linq.IQueryable&gt;'转换到'System.Collections.Generic.IEnumerable'

2 个答案:

答案 0 :(得分:1)

试试这个

foreach(var x in mydetial)
{
  _context.PortServicesTariffDetails.RemoveRange(x.ToList());
}

 _context.SaveChanges();

但是我不明白你为什么要把所有东西分组来删除它?

答案 1 :(得分:0)

试试这个:

var detalis = _context.PortServicesTariffDetails
                                   .Where(w => w.PortServicesTariffId == TariffId &&
                                          w.PortServicesTariffDetailId == DetailId).ToList();

if (detalis.Count() > 0)
{
 var mydetial = detalis.GroupBy(g => new { g.StartRate,g.EndRate }).AsQueryable();
 _context.PortServicesTariffDetails.RemoveRange(mydetial.AsQueryable());
 _context.SaveChanges();
}

我认为没有必要使用Group by, 您只需使用RemoveRange

即可
_context.PortServicesTariffDetails.RemoveRange(detalis);
 _context.SaveChanges();