Skip to content

Instantly share code, notes, and snippets.

@yicone
Last active December 14, 2015 00:49
Show Gist options
  • Select an option

  • Save yicone/5001364 to your computer and use it in GitHub Desktop.

Select an option

Save yicone/5001364 to your computer and use it in GitHub Desktop.
这两段代码哪个好理解,没有if的那个吗
var list = query.ToList();
var list2 = query2.ToList();
List<string> idsWillRemove = new List<string>();
foreach (var p1 in list)
{
var p2 = list2.SingleOrDefault(plan2 => plan2.ShoppingInfoId == p1.ShoppingInfoId);
if (p2 == null)
{
idsWillRemove.Add(p1.ShoppingInfoId);
}
else
{
p1.TotalPrice += p2.TotalPrice;
p1.TotalFuelSurcharges += p2.TotalFuelSurcharges;
p1.TotalTax += p2.TotalTax;
p1.AdultCount += p2.AdultCount;
p1.AdultFuelSurcharges += p2.AdultFuelSurcharges;
p1.AdultPrice += p2.AdultPrice;
p1.AdultTax += p2.AdultTax;
p1.ChildCount += p2.ChildCount;
p1.ChildFuelSurcharges += p2.ChildFuelSurcharges;
p1.ChildTax += p2.ChildTax;
p1.ChildPrice += p2.ChildPrice;
}
}
list.RemoveAll(p1 => idsWillRemove.Contains(p1.ShoppingInfoId));
var list = new List<FlightSegmentPlan>();
var list1 = query.ToList();
var list2 = query2.ToList();
// 按ShoppingInfoId取交集
var idIntersection = list1.Select(p1 => p1.ShoppingInfoId).Intersect(list2.Select(p2 => p2.ShoppingInfoId));
foreach (var id in idIntersection)
{
var p1 = list1.Single(p => p.ShoppingInfoId == id);
var p2 = list2.Single(p => p.ShoppingInfoId == id);
p1.TotalPrice += p2.TotalPrice;
p1.TotalFuelSurcharges += p2.TotalFuelSurcharges;
p1.TotalTax += p2.TotalTax;
p1.AdultCount += p2.AdultCount;
p1.AdultFuelSurcharges += p2.AdultFuelSurcharges;
p1.AdultPrice += p2.AdultPrice;
p1.AdultTax += p2.AdultTax;
p1.ChildCount += p2.ChildCount;
p1.ChildFuelSurcharges += p2.ChildFuelSurcharges;
p1.ChildTax += p2.ChildTax;
p1.ChildPrice += p2.ChildPrice;
list.Add(p1);
}
@vwxyzh
Copy link
Copy Markdown

vwxyzh commented Feb 21, 2013

    [TestMethod]
    public void Test1()
    {
        IEnumerable<FlightSegmentPlan> query1 = new FlightSegmentPlan[] { };
        IEnumerable<FlightSegmentPlan> query2 = new FlightSegmentPlan[] { };
        var list = (from item in query1.Concat(query2)
                    group item by item.ShoppingInfoId into g
                    select g.Aggregate((p1, p2) => p1 + p2)).ToList();
    }

    public class FlightSegmentPlan
    {
        public int ShoppingInfoId, TotalPrice, TotalFuelSurcharges, TotalTax, AdultCount, AdultFuelSurcharges, AdultPrice, AdultTax, ChildCount, ChildFuelSurcharges, ChildTax, ChildPrice;
        public static FlightSegmentPlan operator +(FlightSegmentPlan plan1, FlightSegmentPlan plan2)
        {
            if (plan1.ShoppingInfoId != plan2.ShoppingInfoId)
                throw new ArgumentException("Not same shopping info!");
            return new FlightSegmentPlan
            {
                ShoppingInfoId = plan1.ShoppingInfoId,
                TotalPrice = plan1.TotalPrice + plan2.TotalPrice,
                TotalFuelSurcharges = plan1.TotalFuelSurcharges + plan2.TotalFuelSurcharges,
                TotalTax = plan1.TotalTax + plan2.TotalTax,
                AdultCount = plan1.AdultCount + plan2.AdultCount,
                AdultFuelSurcharges = plan1.AdultFuelSurcharges + plan2.AdultFuelSurcharges,
                AdultPrice = plan1.AdultPrice + plan2.AdultPrice,
                AdultTax = plan1.AdultTax + plan2.AdultTax,
                ChildCount = plan1.ChildCount + plan2.ChildCount,
                ChildFuelSurcharges = plan1.ChildFuelSurcharges + plan2.ChildFuelSurcharges,
                ChildTax = plan1.ChildTax + plan2.ChildTax,
                ChildPrice = plan1.ChildPrice + plan2.ChildPrice,
            };
        }
    }

@vwxyzh
Copy link
Copy Markdown

vwxyzh commented Feb 21, 2013

        // or more simple one:
        IEnumerable<FlightSegmentPlan> query1 = new FlightSegmentPlan[] { };
        IEnumerable<FlightSegmentPlan> query2 = new FlightSegmentPlan[] { };
        var list = (from item in query1.Concat(query2)
                    group item by item.ShoppingInfoId into g
                    select new FlightSegmentPlan
                    {
                        ShoppingInfoId = g.Key,
                        TotalPrice = g.Sum(x => x.TotalPrice),
                        TotalFuelSurcharges = g.Sum(x => x.TotalFuelSurcharges),
                        // ...
                    }).ToList();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment