Last active
December 14, 2015 00:49
-
-
Save yicone/5001364 to your computer and use it in GitHub Desktop.
这两段代码哪个好理解,没有if的那个吗
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
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