I'm fairly new to programming but have been tasked with maintaining some applications that were created by a previous employee. I have a ?: statement that now needs to handle more than a true or false statement but I'm not sure how to go about doing it. The code in question is:
MailDomainContext mail = new MailDomainContext();
mail.Load(mail.GetMailsQuery("Workforce Attendence Issue",
loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().Username,
(loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().EmployeeShiftID >= 2 ? "supervisor1" : "supervisor2"),
loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().FirstName,
attendence.AttendenceDate.ToString("MM/dd/yyyy"),
attendence.TimeLost,
loadAbs.Entities.Where(abs => abs.AbsenceID == attendence.AbsenceID).First().AbsenceDescription,
(from inf in loadAtt.Entities
where inf.EmployeeID == _EmployeeID
where inf.AttendenceDate > DateTime.Now.AddDays(30 * -1)
where inf.Approved == false
select inf).Count() + 1,
attendence.UTOUsed
), null, null);
More specifically this line:
(loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().EmployeeShiftID >= 2 ? "supervisor1" : "supervisor2"),
I need to add 4 more supervisors to the list but haven't figured out a way to do it that doesn't make everything else unhappy. I apologize if this is too simple a question or if I left out some details you might need to know, as I said I'm pretty new to all of this.
This code is needlessly hard to maintain and also inefficient and not very defensive. The code is retrieving the employee three times.
loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().Username
The above line (and others) will throw an exception if the employee with _EmployeeID doesn't exist. Instead, you could use FirstOrDefault, or SingleOrDefault if you expect there to only ever be one employee with that ID (which should be the case as it looks like primary key for that entity). If loadEmp is actually an Entity Framework DbContext then you could also use Find.
You can do this query once and store the result in a local variable.
var employee = loadEmp.Entities.SingleOrDefault(emp => emp.EmployeeID == _EmployeeID);
if (employee == null)
{
// Handle employee not found
}
To then get the supervisor string based on the employee, you could create a method which takes the minimum amount of information needed to calculate the supervisor string, and pass this into the method to get your result.
GetSupervisorRole(employee.EmployeeShiftID);
...
private string GetSupervisorRole(int employeeShiftID)
{
// Logic here
}
One approach is to extract that code into a method and write that method any way you want.
Another approach is to use dictionary to map keys (if you have small number of them) to values.
var id =3;
var mapping = new Dictionary<int, string>() {
{ 1, "first" },
{ 2, "second" },
{ 3, "first" } //you can map 2 values (1,3) to the same "first" string
};
string value;
if (!mapping.TryGetValue(id, out value))
{
value = "unknown";
}
Create the following method:
string GetSupervisor(int employeeShiftId) {
if (employeeShiftId == 1) supervisor = "supervisor1";
else if (employeeShiftId == 2) supervisor = "supervisor2";
else if (employeeShiftId == 3) supervisor = "supervisor3";
else if (employeeShiftId == 4) supervisor = "supervisor4";
}
Then call it from your code and assign the result to a variable supervisor, which you can then use in mail.Load():
int employeeShiftId = loadEmp.Entities
.Where(emp => emp.EmployeeID == _EmployeeID).First()
.EmployeeShiftID;
string supervisor = GetSupervisor(employeeShiftId);
MailDomainContext mail = new MailDomainContext();
mail.Load(mail.GetMailsQuery("Workforce Attendence Issue",
loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().Username,
supervisor, // <-- Then use it here
...
);
I would replace this whole section
loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().Username,
(loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().EmployeeShiftID >= 2 ? "supervisor1" : "supervisor2"),
loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().FirstName,
with
//Assign these variables ahead of time so others reading your code can
//figure out what's going on
var EmpID = loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First();
var UserName = EmpID.UserName;
var FirstName = EmpID.FirstName;
var Title = GetTitle(EmpID.EmployeeShiftID);
//Your original call
Mail.Load(mail.GetMailsQuery("Workforce Attendence Issue",
UserName,
Title,
FirstName,
//...Etc
);
//New method you will need to add, you could do this logic in line, but this will
//be less messy
private string GetTitle(int ShiftID)
{
switch (ShiftID)
{
case 1:
return "supervisor1";
break;
case 2:
return "supervisor2";
break;
//...etc
}
}
Not that this is a great idea, but you can combine inline if's as so:
static void Main(string[] args)
{
int EmpId = 2;
string supervisor = EmpId == 4 ? "Supervisor4" :
EmpId == 3 ? "Supervisor3" :
EmpId == 2 ? "supervisor2" :
"supervisor1" ;
Console.WriteLine(supervisor);
}
You can see another example of this in another stack question: Legible or not: C# multiple ternary operators + Throw if unmatched
I would probably instead go for the Method approach like KDiTraglia proposed where you just pass in the EmpId and get the supervisor name back, or alternatively the Dictionary lookup approach like Alexei Levenkov propsed though.
Related
i have a table in which i have record of users.table have field lastLoginMonth and LastLoginYear ...i want to fetch that user who have login time more than 5 month ..but here i found two case ..
1)current year and lastLoginYear same
2)current year and lastLoginYear different
to handle this i have to use different conditions but i don't know how to handle this in query.....
var year = db.UserManagers.ToList();
foreach (var y in year)
{
if (y.LastLoginYear == mydate.Year)
{
var modell = (from ummm in db.UserManagers
where ((mydate.Month - ummm.LastLoginMonth) >5
&& ummm.LoginWarning==false)
select ummm).ToList();
return View(modell);
}
var model = (from ummm in db.UserManagers
where (((12 - y.LastLoginMonth) + mydate.Month) >5
&& ummm.LoginWarning == false)
select ummm).ToList();
return View(model);
}
how i can organize this query in a simple way ...
Use ternary operator:
var modell = (from ummm in db.UserManagers
where (((y.LastLoginYear == mydate.Year)
? ((mydate.Month - ummm.LastLoginMonth) >5)
: ((12 - y.LastLoginMonth) + mydate.Month) >5)
&& ummm.LoginWarning==false)
select ummm).ToList();
Take a look at this example to understand what does this mean like:
var list = new List<string> { "1", "abc", "5"};
var sel = (from s in list where ((s.Length > 1) ? true : false) select s);
As you can see, we take each string s stored in the list and apply to it the next filter: If it’s Length more then 1, we take it (as it will be where true), otherwise, we don’t take it. Thus we will take only those strings that have Length more then 1.
Also pay attention that you make return inside the foreach loop. That means that the foreach will iterate only 1 time and then will exit on the return you wrote. So you might expect this code to make something different from what you have written.
First approach that came to my mind could be with simple inline if:
var year = db.UserManagers.ToList();
foreach (var y in year)
{
var model = (from ummm in db.UserManagers
where (((y.LastLoginYear == mydate.Year)?(mydate.Month - ummm.LastLoginMonth):((12 - y.LastLoginMonth) + mydate.Month)) >5 && ummm.LoginWarning==false)
select ummm).ToList();
return View(model);
}
}
I assume that you realize that a return inside a foreach loop would make it execute only once and then return the result?
Let me first explain what I'm trying to accomplish.
I'm working with a C# ASP.NET MVC 5 project using Entity Framework to communicate with a SQL Server database. Most of the queries utilizes linq for its queries. In various places on the frontend site I'm displaying lists of records and need to provide the means of searching these records via a search bar. The initial idea right now is to allow the user to enter a search phrase with keywords being separated by spaces, and those keywords are used to match any combination of fields in the records of a table.
For example, say my search is "John Doe" against a user table. Consider these being the records in this table:
uFirstName uLastName
---------- ----------
Johnny Doe
John Doe
Jane Doe
The first two records should be returned.
Here's an example method I would call to return the results I expect:
public static List<UserModel> GetUserList(string terms)
{
using (DBConnection dbcontext = new DBConnection())
{
var termlist = (terms == "") ? new List<string>() : terms.Split(' ').ToList();
var linqList = (from u in dbcontext.Users
where
(
(terms == "") ||
(termlist.Any(_s => u.uLastName.Contains(_s))) ||
(termlist.Any(_s => u.uFirstName.Contains(_s)))
)
select new { u.uLastName, u.uFirstName });
return linqList.ToList().ConvertAll<UserModel> ( u => new UserModel { LastName = u.uLastName, FirstName = u.uFirstName } );
}
}
In my project I'm utilizing this search bar in various places being used to search against a variety of tables that obviously have different fields. What I would like to do is create a helper method that allows me to pass in the "terms" string and have it matched against a list of field values within the linq statement generically. Here's an example pseudo method that shows what I would like to change the above method to:
public static List<UserModel> GetUserList(string terms)
{
using (DBConnection dbcontext = new DBConnection())
{
var linqList = (from u in dbcontext.Users
where SearchTermMatch(terms, new List<string>() { u.uLastName, u.uFirstName }) == true
select new { u.uLastName, u.uFirstName });
return linqList.ToList().ConvertAll<UserModel>(u => new UserModel { LastName = u.uLastName, FirstName = u.uFirstName });
}
}
And this is what the helper method would look like:
public static bool SearchTermMatch(string terms, List<string> fieldvalues)
{
if (terms == "") return true;
else
{
var termlist = terms.Split(' ').ToList();
var foundlist = new List<bool>();
foreach (string value in fieldvalues)
foundlist.Add(termlist.Any(s => value.Contains(s)));
return foundlist.Any(f => f == true);
}
}
Even though this compiles fine, at runtime it produces the following error:
LINQ to Entities does not recognize the method 'Boolean SearchTermMatch(System.String, System.Collections.Generic.List`1[System.String])' method, and this method cannot be translated into a store expression.
From all my searching on how to get this working, it's clear I need to utilize Expressions, but I can't for the life of me understand how those work. What I do understand is that Entity Framework wants to convert the linq statements into a query that SQL can understand, and my helper method isn't equipped to do so.
Ultimately what I want to accomplish is to build a helper method that I can later expand upon with more advanced searching techniques. I figure if I start simple with a search on all relevant fields based on a keyword split, I can later add more complexity that I would only have to do to this helper method and all my search bars will benefit from those advancements.
So I guess what I'm looking for is your help on how I can create this helper method that I can use throughout my various linq statements in my project.
Ok, I found a solution to my question. It's not completely ideal, but it gets the job done.
Let me first give reference to the sources I'm using for my solution. I first referred to this answer as the starting point:
https://stackoverflow.com/a/27993416/4566281
This answer referred to a source that I ended up using in my project. If you're using Visual Studio, you can find the package in NuGet, just search for "neinlinq", or get it from this GitHub repository:
https://github.com/axelheer/nein-linq
The only reason I don't consider this my ideal solution is that I was hoping to stick completely to the libraries in .NET / MVC. There's nothing wrong with using a 3rd party library, and in this case, it got the job done for me. But I was hoping to accomplish this as native as possible, and within reason.
So on to my code solution, as I hope this will help someone else in some capacity.
My "helper" function(s) ended up being this (don't forget to include "using NeinLinq;")
[InjectLambda]
public static bool SearchTermMatch(List<string> termlist, List<string> fieldvalues)
{
throw new NotImplementedException();
}
public static Expression<Func<List<string>, List<string>, bool>> SearchTermMatch()
{
return (t,f) =>
(
(t.Count() == 0) ||
(t.Count(_t => f.Any(_f => _f.Contains(_t)) || _t == "") == t.Count())
);
}
And, my linq statement ended up being the following:
public static List<UserModel> GetUserList(string terms)
{
using (DBConnection dbcontext = new DBConnection())
{
var termlist = (terms == "") ? new List<string>() : terms.Split(' ').ToList();
var linqList = (from u in dbcontext.Users
where SearchTermMatch(termlist, new List<string>() { u.uLastName, u.uFirstName })
select new { u.uLastName, u.uFirstName });
return linqList.ToList().ConvertAll<UserModel>(u => new UserModel { LastName = u.uLastName, FirstName = u.uFirstName });
}
}
I also didn't like that I have to construct the "termlist" before the linq statement in order to make the comparisons I wanted. Ideally I'd like to have the "SearchTermMatch" expression to construct the list through something similar to Split so all I had to do was pass in the string "terms", but I couldn't figure out how to accomplish that in the expression. If someone has an idea on how to do that please let me know. I could then have the flexibility to establish my own set of search rules in the expression instead of having the calling linq statement make the list.
So, to come full circle on how this accomplishes my sitution, I can now repurpose SearchTermMatch for all my search bar scenarios. Take for example this statement:
var linqList = (from p in Person
join a in Address on p.AddressID equals a.AddressID
select new { p.ContactName, p.EmailAddress, a.Street, a.City, a.State, a.Zipcode });
I can now easily update it to the following to handle my search bar call:
var termlist = (terms == "") ? new List<string>() : terms.Split(' ').ToList();
var linqList = (from p in Person
join a in Address on p.AddressID equals a.AddressID
where SearchTermMatch(termlist, new List<string>() { p.ContactName, p.EmailAddress, a.Street, a.City, a.State, a.Zipcode })
select new { p.ContactName, p.EmailAddress, a.Street, a.City, a.State, a.Zipcode });
I have a form with multiple search criteria that a user can use to search for employee data, e.g. FirstName, LastName, HireDate, Department, etc.
I am using LINQ and am wondering what method could I use to query a collection of Employes given any of of the search criteria, i.e. a user does not have to enter all, but they do have to enter at least one of the search parameters.
So far, while testing my LINQ statement with two search parameters in place, it seems that I have to see if the search parameter is entered or not.
If this is the case, then this can get quite unwieldy for many search parameters.
// only FirstName is entered
if (!string.IsNullOrEmpty(FirstName) && string.IsNullOrEmpty(LastName))
{
var employees = DB.Employees
.Where(emp => emp.FirstName.Contains(fName));
}
// only LastName is entered
else if (string.IsNullOrEmpty(FirstName) && !string.IsNullOrEmpty(LastName))
{
var employees = DB.Employees
.Where(emp => emp.LastName.Contains(lName));
}
// both parameters are entered
else if (!string.IsNullOrEmpty(FirstName) && !string.IsNullOrEmpty(LastName))
{
var employees = DB.Employees
.Where(emp => emp.FirstName.Contains(fName))
.Where(emp => emp.LastName.Contains(lName));
}
FYI, I initially thought that I could just append Where() statements to my LINQ statement with the pertinent search parameters but I noticed that not all records were being returned that should and thus the above logic of if-then statements.
What about something like this:
IQueryable<Employee> employees = DB.Employees;
if (!string.IsNullOrEmpty(FirstName))
{
employees = employees
.Where(emp => emp.FirstName.Contains(fName));
}
if (!string.IsNullOrEmpty(LastName))
{
employees = employees
.Where(emp => emp.Last.Contains(lName));
}
You can write it like this:
var employees = DB.Employees.AsQueryable();
if (!string.IsNullOrEmpty(fName)
employees = employees.Where(emp => emp.FirstName.Contains(fName));
if (!string.IsNullOrEmpty(lName)
employees = employees.Where(emp => emp.LastName.Contains(lName));
I encountered a similar challenge where a user could select 0, 1 or many values for about 10 searchable fields and needed to construct that query at runtime.
I ended up using LINQKit:
http://www.albahari.com/nutshell/linqkit.aspx
In particular I used it's predicate builder, which is described here:
http://www.albahari.com/nutshell/predicatebuilder.aspx
In your example above, you've encompassed the query multiple within if statements.
The alternative is to build the query as you go.
If you were to declare var employees = DB.Employees outside of those if statements (Assuming that it's always relevant), then you could just tack on your where statements within your if statements if they're applicable.
LINQ gives you deferred execution, so you don't have to have the entire expression in a single block (Even though it feels most natural to do so and in many cases you will).
Things get a bit more complicated if you want to mix in OR's with ANDs, but that's where the previously mentioned predicate builder comes in.
Unfortunately I don't have any examples to share, but those links should get you off to a good start.
var resultData = (from data in db.Abc
where !string.IsNullOrEmpty(firstName) ? data.FirstName == firstName : true
&& data.UserType == userTypeValue
&& !string.IsNullOrEmpty(lastName) ? data.LastName == lastName : true
&& !string.IsNullOrEmpty(gender) ? data.Gender == gender : true
&& !string.IsNullOrEmpty(phone) ? data.CellPhone == phone : true
&& !string.IsNullOrEmpty(fax) ? data.Fax == fax : true
&& !string.IsNullOrEmpty(emailAddress) ? data.Email == emailAddress : true
&& !string.IsNullOrEmpty(address1) ? data.Address == address1 : true
select new
{
UserName = data.UserName,
FirstName = data.FirstName,
Address = data.Address,
CellPhone = data.CellPhone,
Fax = data.Fax,
Email = data.Email
}).ToList();
I'm working in MVC3 project. I was browsing for a while and trying several examples but I could not get it working.
I need to get a list of record from OrderForm table whose DeptID are in another list I already have got.
I'm aware that I need to use Contains() replacing IN SQL clause, but every example that I could read are doing this in the same way
.Where(ListOfDepartments.Contains(q.DeptID))
This is my method at the controller, which obviously is not working:
public ActionResult ValidOrders(string installation, string orderpriority, string stockclass, string validity)
{
int instID = System.Convert.ToInt32(installation);
int orderpriorityID = System.Convert.ToInt32(orderpriority);
int stockclassID = System.Convert.ToInt32(stockclass);
string period = validity;
try
{
var departments = dba.Department
.Where (a => a.InstID == instID);
var valid = dba.OrderForm
.Where(q => q.FormType == 3
&& q.FormStatus == 2
&& q.OrderPriority.OrderPriorityID == orderpriorityID
&& q.StockClassID == stockclassID
&& departments.Contains(q.DeptID));
return View(valid.ToList());
}
catch (Exception)
{
return View("Error");
}
}
What I'm doing wrong?
you need a list of int, not Department.
var departments = dba.Department
.Where (a => a.InstID == instID)
.Select(d => d.Id);//Id is a guess, it maybe another property name
//.ToList();
I'm trying to select a subgroup of a list where items have contiguous dates, e.g.
ID StaffID Title ActivityDate
-- ------- ----------------- ------------
1 41 Meeting with John 03/06/2010
2 41 Meeting with John 08/06/2010
3 41 Meeting Continues 09/06/2010
4 41 Meeting Continues 10/06/2010
5 41 Meeting with Kay 14/06/2010
6 41 Meeting Continues 15/06/2010
I'm using a pivot point each time, so take the example pivot item as 3, I'd like to get the following resulting contiguous events around the pivot:
ID StaffID Title ActivityDate
-- ------- ----------------- ------------
2 41 Meeting with John 08/06/2010
3 41 Meeting Continues 09/06/2010
4 41 Meeting Continues 10/06/2010
My current implementation is a laborious "walk" into the past, then into the future, to build the list:
var activity = // item number 3: Meeting Continues (09/06/2010)
var orderedEvents = activities.OrderBy(a => a.ActivityDate).ToArray();
// Walk into the past until a gap is found
var preceedingEvents = orderedEvents.TakeWhile(a => a.ID != activity.ID);
DateTime dayBefore;
var previousEvent = activity;
while (previousEvent != null)
{
dayBefore = previousEvent.ActivityDate.AddDays(-1).Date;
previousEvent = preceedingEvents.TakeWhile(a => a.ID != previousEvent.ID).LastOrDefault();
if (previousEvent != null)
{
if (previousEvent.ActivityDate.Date == dayBefore)
relatedActivities.Insert(0, previousEvent);
else
previousEvent = null;
}
}
// Walk into the future until a gap is found
var followingEvents = orderedEvents.SkipWhile(a => a.ID != activity.ID);
DateTime dayAfter;
var nextEvent = activity;
while (nextEvent != null)
{
dayAfter = nextEvent.ActivityDate.AddDays(1).Date;
nextEvent = followingEvents.SkipWhile(a => a.ID != nextEvent.ID).Skip(1).FirstOrDefault();
if (nextEvent != null)
{
if (nextEvent.ActivityDate.Date == dayAfter)
relatedActivities.Add(nextEvent);
else
nextEvent = null;
}
}
The list relatedActivities should then contain the contiguous events, in order.
Is there a better way (maybe using LINQ) for this?
I had an idea of using .Aggregate() but couldn't think how to get the aggregate to break out when it finds a gap in the sequence.
Here's an implementation:
public static IEnumerable<IGrouping<int, T>> GroupByContiguous(
this IEnumerable<T> source,
Func<T, int> keySelector
)
{
int keyGroup = Int32.MinValue;
int currentGroupValue = Int32.MinValue;
return source
.Select(t => new {obj = t, key = keySelector(t))
.OrderBy(x => x.key)
.GroupBy(x => {
if (currentGroupValue + 1 < x.key)
{
keyGroup = x.key;
}
currentGroupValue = x.key;
return keyGroup;
}, x => x.obj);
}
You can either convert the dates to ints by means of subtraction, or imagine a DateTime version (easily).
In this case I think that a standard foreach loop is probably more readable than a LINQ query:
var relatedActivities = new List<TActivity>();
bool found = false;
foreach (var item in activities.OrderBy(a => a.ActivityDate))
{
int count = relatedActivities.Count;
if ((count > 0) && (relatedActivities[count - 1].ActivityDate.Date.AddDays(1) != item.ActivityDate.Date))
{
if (found)
break;
relatedActivities.Clear();
}
relatedActivities.Add(item);
if (item.ID == activity.ID)
found = true;
}
if (!found)
relatedActivities.Clear();
For what it's worth, here's a roughly equivalent -- and far less readable -- LINQ query:
var relatedActivities = activities
.OrderBy(x => x.ActivityDate)
.Aggregate
(
new { List = new List<TActivity>(), Found = false, ShortCircuit = false },
(a, x) =>
{
if (a.ShortCircuit)
return a;
int count = a.List.Count;
if ((count > 0) && (a.List[count - 1].ActivityDate.Date.AddDays(1) != x.ActivityDate.Date))
{
if (a.Found)
return new { a.List, a.Found, ShortCircuit = true };
a.List.Clear();
}
a.List.Add(x);
return new { a.List, Found = a.Found || (x.ID == activity.ID), a.ShortCircuit };
},
a => a.Found ? a.List : new List<TActivity>()
);
Somehow, I don't think LINQ was truly meant to be used for bidirectional-one-dimensional-depth-first-searches, but I constructed a working LINQ using Aggregate. For this example I'm going to use a List instead of an array. Also, I'm going to use Activity to refer to whatever class you are storing the data in. Replace it with whatever is appropriate for your code.
Before we even start, we need a small function to handle something. List.Add(T) returns null, but we want to be able to accumulate in a list and return the new list for this aggregate function. So all you need is a simple function like the following.
private List<T> ListWithAdd<T>(List<T> src, T obj)
{
src.Add(obj);
return src;
}
First, we get the sorted list of all activities, and then initialize the list of related activities. This initial list will contain the target activity only, to start.
List<Activity> orderedEvents = activities.OrderBy(a => a.ActivityDate).ToList();
List<Activity> relatedActivities = new List<Activity>();
relatedActivities.Add(activity);
We have to break this into two lists, the past and the future just like you currently do it.
We'll start with the past, the construction should look mostly familiar. Then we'll aggregate all of it into relatedActivities. This uses the ListWithAdd function we wrote earlier. You could condense it into one line and skip declaring previousEvents as its own variable, but I kept it separate for this example.
var previousEvents = orderedEvents.TakeWhile(a => a.ID != activity.ID).Reverse();
relatedActivities = previousEvents.Aggregate<Activity, List<Activity>>(relatedActivities, (items, prevItem) => items.OrderBy(a => a.ActivityDate).First().ActivityDate.Subtract(prevItem.ActivityDate).Days.Equals(1) ? ListWithAdd(items, prevItem) : items).ToList();
Next, we'll build the following events in a similar fashion, and likewise aggregate it.
var nextEvents = orderedEvents.SkipWhile(a => a.ID != activity.ID);
relatedActivities = nextEvents.Aggregate<Activity, List<Activity>>(relatedActivities, (items, nextItem) => nextItem.ActivityDate.Subtract(items.OrderBy(a => a.ActivityDate).Last().ActivityDate).Days.Equals(1) ? ListWithAdd(items, nextItem) : items).ToList();
You can properly sort the result afterwards, as now relatedActivities should contain all activities with no gaps. It won't immediately break when it hits the first gap, no, but I don't think you can literally break out of a LINQ. So it instead just ignores anything which it finds past a gap.
Note that this example code only operates on the actual difference in time. Your example output seems to imply that you need some other comparison factors, but this should be enough to get you started. Just add the necessary logic to the date subtraction comparison in both entries.