score:1
That is somewhat dirty but can help! You can just skip elements that you're not interested in while keeping some flag element. And it's only one enumeration, so O(n) complexity
public static IEnumerable<Availability> GetMerged(List<Availability> oldList)
{
var oldValue = oldList[0];
foreach (var currentValue in oldList)
{
if (IsDifferent(oldValue, currentValue))
{
yield return oldValue;
oldValue = currentValue;
}
}
yield return oldValue;
}
public static bool IsDifferent(Availability x, Availability y)
{
if (x.Accessibility != y.Accessibility || x.Rate != y.Rate || x.RoomTypeId != y.RoomTypeId)
return true;
if (Math.Abs((x.DateFrom - x.DateTo).TotalDays) > 1)
return true;
return false;
}
You can also go by LINQ group by method and by overriding custom comparer, but you should pick right and custom GetHashCode function and i've found that difficult for now.
score:1
If you install the Nuget package MoreLINQ you can use the Segment()
method for that:
var mergedAvails = avails
.GroupBy(x => new { x.RoomTypeId, x.Rate, x.Accessibility })
.SelectMany(groupedAvails => groupedAvails
.OrderBy(avail => avail.DateFrom)
.Segment((curr, prev, _) => prev.DateTo.AddDays(1) != curr.DateFrom)
.Select(consecutiveAvails => new Availability {
Accessibility = consecutiveAvails.First().Accessibility,
RoomTypeId = consecutiveAvails.First().RoomTypeId,
Rate = consecutiveAvails.First().Rate,
DateFrom = consecutiveAvails.First().DateFrom,
DateTo = consecutiveAvails.Last().DateTo
}));
The general idea is that you first group all items by the properties you specified and then order the items in each group by date. Segement()
will then split each group into sub-groups where each sub-group only contains items with consecutive days.
I didn't test it, but the code should also work for items that span multiple days instead of just one (as long as the date ranges don't overlap between different items).
By the way, I also work for the tourism industry and we do the same in some places of our C# software ^^
Working example: https://dotnetfiddle.net/XVkOZD
Source: stackoverflow.com
Related Query
- Merge list elements if date differs by one day
- How to merge more than one list having objects into one list with sum of one of the elements of the list in c#
- Merge two collections, in case of duplicate elements pick the one with the latest date
- Merge two List<object> into one List in Linq
- Merge multiple Lists into one List with LINQ
- Check if one list contains any elements from another
- Using Linq query inside List<T>.AddRange to conditionally add elements from one list to another
- Merge two Lists in C# and merge objects with the same id into one list item
- C# - Merge list items into one item based on some matching values
- LINQ in C#. Check if one list contains elements from another one
- Copy a list of objects to another typed list in one line of code
- How to Remove elements from one List based another list's element and condition?
- How to merge two lists of different types into one list of new type that contains both types?
- Check which elements are on one list comparing to another list LINQ
- Elegant way to check if a list contains an object where one property is the same, and replace only if the date of another property is later
- Get those elements from a List of custom class objects whose one property value is parseable to double
- Check if elements from one list elements present in another list
- find list elements which are similar to other list elements by 3 properties and then add value from the second one to the first
- Use linq to remove elements in one list using a condition in another
- Merge the values of multiple dictionaries into one list
- Compare elements of an object resides in two list - one on one using LINQ
- How to sort list on multiple properties in one line of code in Linq
- Construct a LINQ query to return an item from a List that is related by one property to other members but which differs by a second property
- Remove elements of one list from other list
- Merge many Lists into one list with properties for each list
- Construct a list of wpf Hyperlink elements from an XML source file using Linq
- List or Array of String Contain specific word in Html Source Code
- Merge nested objects in one list using Linq
- merge items into one that are repeated in the same list
- Match data from two list<T> where date from one list falls between dates from second list
More Query from same tag
- Get total time duration by group from list
- Accessing XML elements with LINQ using VB.net (not C#)
- Query in LINQ to XML?
- Failed to read attributes from XML file
- Get the id of the latest record in group by with linq
- Distinct by part of the string in linq
- How to expand calling expressions other than predicates?
- Ability to conditionally choose what row to select
- GROUPBY and SUM on List items using LINQ
- Aggregate rows in a DataTable using LINQ
- LINQ Parsing XML
- C# Linq how to add each missing year and month to data
- Unexpected difference between IEnumerable and List as result of a linq-statement in .net-core?
- wrong output when getting two columns of a table with linq
- Best-Practice for adding an empty row to a databound Listbox using Linq2SQL & Winforms
- Dynamic LINQ Query not working properly with Taxonomy field
- How do I retrieve the value of an attribute in a parent if one of the elements of a child equals to a value?
- How can I insert 2 or 3 rows to the same table using linq
- MonogDB C# Driver LINQ query not working when filtering by _id
- Forced to use Linq on a custom settings file, but can't figure it out
- Trouble With Linq Where Clause
- LINQ Join Query Structure Resulting in CS0119 Error
- Navigating project files in XML
- Syntax for "and" and distinct operation in entity framework?
- Convert Function Based on TSQL with Function Based with Entity Framework
- Generate serial no. in List using LINQ
- Read and Write XML in C# using Linq
- Filter data quarterly using LinQ
- Extracting a tree structure
- When Where clause is used inside Linq statement produces different results than when used outside