0

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

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

我很担心使用“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));
4

3 回答 3

4

您可以使用Except

var remaining = defaults.Except(notDefaults);

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

请参阅此处 http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

于 2013-08-01T13:47:51.743 回答
1

您正在寻找的是这两组的补充。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());

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

于 2013-08-01T13:59:30.703 回答
0
var notDefaultIds = notDefaults.Select(nd => nd.AssetID);
var result = defaults.Where(def => !notDefaultIds.Any(id => id == def.AssetID);

如果您想减少 db 往返,请ToList()notDefaults,移动defaultsresult;

于 2013-08-01T13:55:17.150 回答