使用linq排除项目

时间:2013-08-01 13:46:39

标签: c# performance linq

我有两个通过LINQ返回的列表。我的目标是返回“默认值”列表中的所有项目减去“notDefaults”列表中的内容。

我目前通过两个嵌套的for循环并使用'RemoveAt'来完成此操作。这只能用一个LINQ语句完成吗?基本上,我需要“默认值”列表中的所有内容,除非该项目存在于“AssetPayrollMarkupOverrides”表中。

我很担心自it appears to be slow以来使用'RemoveAt'。

var defaults = (from a in dbcontext.Assets
    join b in dbcontext.AssetAddresses on a.AssetID equals b.AssetID
    join c in dbcontext.Addresses on b.AddressID equals c.AddressID
    join d in dbcontext.StateLookups on c.StateID equals d.StateID
    where d.ShortName == state
    select new AssetMarkup()
    {
        AssetId = a.AssetID,
        AssetName = a.AssetName,
        Seg1_Code = a.Seg1_Code,
    }).ToList();


var notDefaults = (from a in dbcontext.Assets
    join b in dbcontext.AssetAddresses on a.AssetID equals b.AssetID
    join c in dbcontext.Addresses on b.AddressID equals c.AddressID
    join d in dbcontext.StateLookups on c.StateID equals d.StateID
    join e in dbcontext.AssetPayrollMarkupOverrides on a.AssetID equals e.AssetID
    where d.ShortName == state
    select new AssetMarkup()
    {
        AssetId = a.AssetID,
        AssetName = a.AssetName,
        Seg1_Code = a.Seg1_Code,
        ShortName = d.ShortName,
        OfficePercentage = e.OfficePercentage,
        MaintenancePercentage = e.MaintenancePercentage,
        Note = e.Note
    }).ToList();

我最初这样做是为了确保两个列表匹配:

var defaults = (from a in dbcontext.Assets
    join b in dbcontext.AssetAddresses on a.AssetID equals b.AssetID
    join c in dbcontext.Addresses on b.AddressID equals c.AddressID
    join d in dbcontext.StateLookups on c.StateID equals d.StateID
    where d.ShortName == state
    select new AssetMarkup()
    {
        AssetId = a.AssetID,
        AssetName = a.AssetName,
        Seg1_Code = a.Seg1_Code,
        ShortName = d.ShortName,
        OfficePercentage = dbcontext.PayrollMarkups.Where(x => x.StateID == c.StateID).Select(x => x.OfficePercentage).FirstOrDefault(),
        MaintenancePercentage = dbcontext.PayrollMarkups.Where(x => x.StateID == c.StateID).Select(x => x.MaintenancePercentage).FirstOrDefault(),
        Note = dbcontext.AssetPayrollMarkupOverrides.Where(x => x.AssetID == a.AssetID).Select(x => x.Note).FirstOrDefault()
    }).ToList();

var notDefaults = (from a in dbcontext.Assets
   join b in dbcontext.AssetAddresses on a.AssetID equals b.AssetID
   join c in dbcontext.Addresses on b.AddressID equals c.AddressID
   join d in dbcontext.StateLookups on c.StateID equals d.StateID
   join e in dbcontext.AssetPayrollMarkupOverrides on a.AssetID equals e.AssetID
   where d.ShortName == state
   select new AssetMarkup()
   {
       AssetId = a.AssetID,
       AssetName = a.AssetName,
       Seg1_Code = a.Seg1_Code,
       ShortName = d.ShortName,
       OfficePercentage = dbcontext.PayrollMarkups.Where(x => x.StateID == c.StateID).Select(x => x.OfficePercentage).FirstOrDefault(),
       MaintenancePercentage = dbcontext.PayrollMarkups.Where(x => x.StateID == c.StateID).Select(x => x.MaintenancePercentage).FirstOrDefault(),
       Note = dbcontext.AssetPayrollMarkupOverrides.Where(x => x.AssetID == a.AssetID).Select(x => x.Note).FirstOrDefault()
   }).ToList();

return Json(defaults.Except(notDefaults).OrderBy(x => x.AssetName).ToDataSourceResult(request));

3 个答案:

答案 0 :(得分:4)

您可以使用Except

var remaining = defaults.Except(notDefaults);

您可能需要确保AssetMarkup上的相等比较器设置为以非基于参考的方式比较对象。

见这里 http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

答案 1 :(得分:1)

你要找的是两组中的complement。 Linq Except()方法旨在实现此目的(您可以使用带有IEqualityComparer的重载):

public class AssetMarkupComparer : IEqualityComparer<AssetMarkup>
{
    public bool Equals(AssetMarkup am1, AssetMarkup am2)
    {
        return am1.AssetId == am2.AssetId;
    }

    public int GetHashCode(AssetMarkup obj)
    {
        return obj.AssetId.GetHashCode();
    }
}

var complement = defaults.Except(notDefaults, new AssetMarkupComparer());

您需要在比较器方法中考虑空值,但这只是一个骨架示例。

答案 2 :(得分:0)

var notDefaultIds = notDefaults.Select(nd => nd.AssetID);
var result = defaults.Where(def => !notDefaultIds.Any(id => id == def.AssetID);

如果您想减少数据库往返,请将ToList()notDefaultsdefaults移至result;