Iterate through Linq to Entities results until condition is met
up vote
0
down vote
favorite
Here is the scenario, I have a query that returns events in a dat. Starting with shift start (SS) I need to find a shift end (SE) if I find it, move on to the next, if not, generate an error and record the row it happened on.
List<string> errorList = new List<string>();
List<int> errorListRow = new List<int>();
var itCompareDay = (from h in db.DailyGPSTables
where h.EmplID == EmpID
&& (h.EventDateTime >= startDate
&& h.EventDateTime <= endDate)
&& (h.EventType == "SS" || h.EventType == "JS" || h.EventType == "LS" || h.EventType == "LE" || h.EventType == "JE" || h.EventType == "SE")
orderby h.EventDateTime
select h).ToList();
int rowNumber = -1;
foreach (DailyGPSTable e in itCompareDay)
{
rowNumber = rowNumber + 1;
string EOOmessage="";
string eventText="";
int dayCountSs = itCompareDay.Count(k => k.EventType == "SS" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
int dayCountSe = itCompareDay.Count(k => k.EventType == "SE" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
int dayCountJS = itCompareDay.Count(k => k.EventType == "JS" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
// Response.Write("<br> Count of event type is ss on " + e.EventDateTime.Value.ToShortDateString() + " is " + itCompareDay.Count(k => k.EventType == "SS" && k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
if (dayCountSs != dayCountSe)
eventText = "";
if (dayCountSs > dayCountSe)
eventText = "Shift End (SE)";
else
eventText = "Shift Start (SS)";
EOOmessage = " On " + e.EventDateTime.Value.ToShortDateString() + " there is a missing " + eventText;
errorList.Add(EOOmessage);
errorListRow.Add(rowNumber);
The above code returns an error for the entire day, I was heading down this path to find the line where a shift start had no ending tag and highlight that row. The day can have multiple shifts but have to have an ending tag.
if (dayCountSs != dayCountSe)
{
eventText = "";
if (dayCountSs > dayCountSe)
string dayEvents = e.EventType;
Response.Write("SS greater than SE >Event Type " + dayEvents +"<BR>");
for (int i = 0; i < dayCountSs; i++)
Response.Write("Should see a count here " + i);
if (dayEvents[i].ToString() != "SS")
i = i + 1;
else
errorListRow.Add(rowNumber);
eventText = "Shift End (SE)";
else
eventText = "Shift Start (SS)";
EOOmessage = " On " + e.EventDateTime.Value.ToShortDateString() + " there is a missing " + eventText;
c# asp.net linq
add a comment |
up vote
0
down vote
favorite
Here is the scenario, I have a query that returns events in a dat. Starting with shift start (SS) I need to find a shift end (SE) if I find it, move on to the next, if not, generate an error and record the row it happened on.
List<string> errorList = new List<string>();
List<int> errorListRow = new List<int>();
var itCompareDay = (from h in db.DailyGPSTables
where h.EmplID == EmpID
&& (h.EventDateTime >= startDate
&& h.EventDateTime <= endDate)
&& (h.EventType == "SS" || h.EventType == "JS" || h.EventType == "LS" || h.EventType == "LE" || h.EventType == "JE" || h.EventType == "SE")
orderby h.EventDateTime
select h).ToList();
int rowNumber = -1;
foreach (DailyGPSTable e in itCompareDay)
{
rowNumber = rowNumber + 1;
string EOOmessage="";
string eventText="";
int dayCountSs = itCompareDay.Count(k => k.EventType == "SS" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
int dayCountSe = itCompareDay.Count(k => k.EventType == "SE" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
int dayCountJS = itCompareDay.Count(k => k.EventType == "JS" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
// Response.Write("<br> Count of event type is ss on " + e.EventDateTime.Value.ToShortDateString() + " is " + itCompareDay.Count(k => k.EventType == "SS" && k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
if (dayCountSs != dayCountSe)
eventText = "";
if (dayCountSs > dayCountSe)
eventText = "Shift End (SE)";
else
eventText = "Shift Start (SS)";
EOOmessage = " On " + e.EventDateTime.Value.ToShortDateString() + " there is a missing " + eventText;
errorList.Add(EOOmessage);
errorListRow.Add(rowNumber);
The above code returns an error for the entire day, I was heading down this path to find the line where a shift start had no ending tag and highlight that row. The day can have multiple shifts but have to have an ending tag.
if (dayCountSs != dayCountSe)
{
eventText = "";
if (dayCountSs > dayCountSe)
string dayEvents = e.EventType;
Response.Write("SS greater than SE >Event Type " + dayEvents +"<BR>");
for (int i = 0; i < dayCountSs; i++)
Response.Write("Should see a count here " + i);
if (dayEvents[i].ToString() != "SS")
i = i + 1;
else
errorListRow.Add(rowNumber);
eventText = "Shift End (SE)";
else
eventText = "Shift Start (SS)";
EOOmessage = " On " + e.EventDateTime.Value.ToShortDateString() + " there is a missing " + eventText;
c# asp.net linq
2
Can you please provide a simple data example with which the error occurs? Your code readability is... lacking, which is hindering my understanding of the logical succession of operations you expect to have. An MCVE would be allround helpful here.
– Flater
Nov 9 at 15:01
@Hogan My question would be how do I iterate through the days events to find an unpaired SE tag. For example on 11/8/2018 I have 3 shifts and for that day I have 3 instances of the SS tag but only 2 SE tags which means the 3rd shift started but never ended. How do I find the 3rd SS tag to add an error condition to my list and record the row number?
– Doug Farrell
Nov 9 at 15:32
That is the problem you are trying to solve. We don't just write solutions for people here -- we help with questions. In trying to solve that problem did you run into something that did not work as expected. Is there a test case that fails, for example. We can answer a question -- as long as the question isn't -- how do I solve this problem write all the code for me. You have given us some code but no details on the problems you are having. If I have to spend 3 hours to understand the solution to answer your question, I'm not going to. If it takes me 10 mins then I'm happy to help.
– Hogan
Nov 9 at 17:21
You can find a lot of good resources in the help screens about how to ask a good question. In this case I would suggest giving us the input data and the expected output data and what your current code is doing. If you do this I expect someone will answer very quickly. (might even be me)
– Hogan
Nov 9 at 17:23
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Here is the scenario, I have a query that returns events in a dat. Starting with shift start (SS) I need to find a shift end (SE) if I find it, move on to the next, if not, generate an error and record the row it happened on.
List<string> errorList = new List<string>();
List<int> errorListRow = new List<int>();
var itCompareDay = (from h in db.DailyGPSTables
where h.EmplID == EmpID
&& (h.EventDateTime >= startDate
&& h.EventDateTime <= endDate)
&& (h.EventType == "SS" || h.EventType == "JS" || h.EventType == "LS" || h.EventType == "LE" || h.EventType == "JE" || h.EventType == "SE")
orderby h.EventDateTime
select h).ToList();
int rowNumber = -1;
foreach (DailyGPSTable e in itCompareDay)
{
rowNumber = rowNumber + 1;
string EOOmessage="";
string eventText="";
int dayCountSs = itCompareDay.Count(k => k.EventType == "SS" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
int dayCountSe = itCompareDay.Count(k => k.EventType == "SE" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
int dayCountJS = itCompareDay.Count(k => k.EventType == "JS" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
// Response.Write("<br> Count of event type is ss on " + e.EventDateTime.Value.ToShortDateString() + " is " + itCompareDay.Count(k => k.EventType == "SS" && k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
if (dayCountSs != dayCountSe)
eventText = "";
if (dayCountSs > dayCountSe)
eventText = "Shift End (SE)";
else
eventText = "Shift Start (SS)";
EOOmessage = " On " + e.EventDateTime.Value.ToShortDateString() + " there is a missing " + eventText;
errorList.Add(EOOmessage);
errorListRow.Add(rowNumber);
The above code returns an error for the entire day, I was heading down this path to find the line where a shift start had no ending tag and highlight that row. The day can have multiple shifts but have to have an ending tag.
if (dayCountSs != dayCountSe)
{
eventText = "";
if (dayCountSs > dayCountSe)
string dayEvents = e.EventType;
Response.Write("SS greater than SE >Event Type " + dayEvents +"<BR>");
for (int i = 0; i < dayCountSs; i++)
Response.Write("Should see a count here " + i);
if (dayEvents[i].ToString() != "SS")
i = i + 1;
else
errorListRow.Add(rowNumber);
eventText = "Shift End (SE)";
else
eventText = "Shift Start (SS)";
EOOmessage = " On " + e.EventDateTime.Value.ToShortDateString() + " there is a missing " + eventText;
c# asp.net linq
Here is the scenario, I have a query that returns events in a dat. Starting with shift start (SS) I need to find a shift end (SE) if I find it, move on to the next, if not, generate an error and record the row it happened on.
List<string> errorList = new List<string>();
List<int> errorListRow = new List<int>();
var itCompareDay = (from h in db.DailyGPSTables
where h.EmplID == EmpID
&& (h.EventDateTime >= startDate
&& h.EventDateTime <= endDate)
&& (h.EventType == "SS" || h.EventType == "JS" || h.EventType == "LS" || h.EventType == "LE" || h.EventType == "JE" || h.EventType == "SE")
orderby h.EventDateTime
select h).ToList();
int rowNumber = -1;
foreach (DailyGPSTable e in itCompareDay)
{
rowNumber = rowNumber + 1;
string EOOmessage="";
string eventText="";
int dayCountSs = itCompareDay.Count(k => k.EventType == "SS" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
int dayCountSe = itCompareDay.Count(k => k.EventType == "SE" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
int dayCountJS = itCompareDay.Count(k => k.EventType == "JS" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
// Response.Write("<br> Count of event type is ss on " + e.EventDateTime.Value.ToShortDateString() + " is " + itCompareDay.Count(k => k.EventType == "SS" && k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
if (dayCountSs != dayCountSe)
eventText = "";
if (dayCountSs > dayCountSe)
eventText = "Shift End (SE)";
else
eventText = "Shift Start (SS)";
EOOmessage = " On " + e.EventDateTime.Value.ToShortDateString() + " there is a missing " + eventText;
errorList.Add(EOOmessage);
errorListRow.Add(rowNumber);
The above code returns an error for the entire day, I was heading down this path to find the line where a shift start had no ending tag and highlight that row. The day can have multiple shifts but have to have an ending tag.
if (dayCountSs != dayCountSe)
{
eventText = "";
if (dayCountSs > dayCountSe)
string dayEvents = e.EventType;
Response.Write("SS greater than SE >Event Type " + dayEvents +"<BR>");
for (int i = 0; i < dayCountSs; i++)
Response.Write("Should see a count here " + i);
if (dayEvents[i].ToString() != "SS")
i = i + 1;
else
errorListRow.Add(rowNumber);
eventText = "Shift End (SE)";
else
eventText = "Shift Start (SS)";
EOOmessage = " On " + e.EventDateTime.Value.ToShortDateString() + " there is a missing " + eventText;
c# asp.net linq
c# asp.net linq
edited Nov 9 at 14:52
Hogan
54k863100
54k863100
asked Nov 9 at 14:37
Doug Farrell
175
175
2
Can you please provide a simple data example with which the error occurs? Your code readability is... lacking, which is hindering my understanding of the logical succession of operations you expect to have. An MCVE would be allround helpful here.
– Flater
Nov 9 at 15:01
@Hogan My question would be how do I iterate through the days events to find an unpaired SE tag. For example on 11/8/2018 I have 3 shifts and for that day I have 3 instances of the SS tag but only 2 SE tags which means the 3rd shift started but never ended. How do I find the 3rd SS tag to add an error condition to my list and record the row number?
– Doug Farrell
Nov 9 at 15:32
That is the problem you are trying to solve. We don't just write solutions for people here -- we help with questions. In trying to solve that problem did you run into something that did not work as expected. Is there a test case that fails, for example. We can answer a question -- as long as the question isn't -- how do I solve this problem write all the code for me. You have given us some code but no details on the problems you are having. If I have to spend 3 hours to understand the solution to answer your question, I'm not going to. If it takes me 10 mins then I'm happy to help.
– Hogan
Nov 9 at 17:21
You can find a lot of good resources in the help screens about how to ask a good question. In this case I would suggest giving us the input data and the expected output data and what your current code is doing. If you do this I expect someone will answer very quickly. (might even be me)
– Hogan
Nov 9 at 17:23
add a comment |
2
Can you please provide a simple data example with which the error occurs? Your code readability is... lacking, which is hindering my understanding of the logical succession of operations you expect to have. An MCVE would be allround helpful here.
– Flater
Nov 9 at 15:01
@Hogan My question would be how do I iterate through the days events to find an unpaired SE tag. For example on 11/8/2018 I have 3 shifts and for that day I have 3 instances of the SS tag but only 2 SE tags which means the 3rd shift started but never ended. How do I find the 3rd SS tag to add an error condition to my list and record the row number?
– Doug Farrell
Nov 9 at 15:32
That is the problem you are trying to solve. We don't just write solutions for people here -- we help with questions. In trying to solve that problem did you run into something that did not work as expected. Is there a test case that fails, for example. We can answer a question -- as long as the question isn't -- how do I solve this problem write all the code for me. You have given us some code but no details on the problems you are having. If I have to spend 3 hours to understand the solution to answer your question, I'm not going to. If it takes me 10 mins then I'm happy to help.
– Hogan
Nov 9 at 17:21
You can find a lot of good resources in the help screens about how to ask a good question. In this case I would suggest giving us the input data and the expected output data and what your current code is doing. If you do this I expect someone will answer very quickly. (might even be me)
– Hogan
Nov 9 at 17:23
2
2
Can you please provide a simple data example with which the error occurs? Your code readability is... lacking, which is hindering my understanding of the logical succession of operations you expect to have. An MCVE would be allround helpful here.
– Flater
Nov 9 at 15:01
Can you please provide a simple data example with which the error occurs? Your code readability is... lacking, which is hindering my understanding of the logical succession of operations you expect to have. An MCVE would be allround helpful here.
– Flater
Nov 9 at 15:01
@Hogan My question would be how do I iterate through the days events to find an unpaired SE tag. For example on 11/8/2018 I have 3 shifts and for that day I have 3 instances of the SS tag but only 2 SE tags which means the 3rd shift started but never ended. How do I find the 3rd SS tag to add an error condition to my list and record the row number?
– Doug Farrell
Nov 9 at 15:32
@Hogan My question would be how do I iterate through the days events to find an unpaired SE tag. For example on 11/8/2018 I have 3 shifts and for that day I have 3 instances of the SS tag but only 2 SE tags which means the 3rd shift started but never ended. How do I find the 3rd SS tag to add an error condition to my list and record the row number?
– Doug Farrell
Nov 9 at 15:32
That is the problem you are trying to solve. We don't just write solutions for people here -- we help with questions. In trying to solve that problem did you run into something that did not work as expected. Is there a test case that fails, for example. We can answer a question -- as long as the question isn't -- how do I solve this problem write all the code for me. You have given us some code but no details on the problems you are having. If I have to spend 3 hours to understand the solution to answer your question, I'm not going to. If it takes me 10 mins then I'm happy to help.
– Hogan
Nov 9 at 17:21
That is the problem you are trying to solve. We don't just write solutions for people here -- we help with questions. In trying to solve that problem did you run into something that did not work as expected. Is there a test case that fails, for example. We can answer a question -- as long as the question isn't -- how do I solve this problem write all the code for me. You have given us some code but no details on the problems you are having. If I have to spend 3 hours to understand the solution to answer your question, I'm not going to. If it takes me 10 mins then I'm happy to help.
– Hogan
Nov 9 at 17:21
You can find a lot of good resources in the help screens about how to ask a good question. In this case I would suggest giving us the input data and the expected output data and what your current code is doing. If you do this I expect someone will answer very quickly. (might even be me)
– Hogan
Nov 9 at 17:23
You can find a lot of good resources in the help screens about how to ask a good question. In this case I would suggest giving us the input data and the expected output data and what your current code is doing. If you do this I expect someone will answer very quickly. (might even be me)
– Hogan
Nov 9 at 17:23
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Using the same extensions methods from your other question, the same code should work, just for SS/SE instead of JS/JE:
var shiftErrEvents = itCompareDay
.Select((ev, rowNum) => new ev.EventType, ev.EventDateTime, rowNum )
.Where(cd => cd.EventType == "SS" || cd.EventType == "SE")
.GroupByWhile((pd, cd) => pd.EventType == "SS" && cd.EventType == "SE" && pd.EventDateTime.Date == cd.EventDateTime.Date)
.Where(cdg => cdg.Count() != 2)
.SelectMany(cdg => cdg.Select(cd => new cd.rowNum, ErrMsg = cd.EventType == "SE" ? "SE without preceding SS" : "SS without following SE" ));
Note: In both cases, a job or shift that crosses the midnight boundary (hence, is on different days) will be handled as two mismatched events.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Using the same extensions methods from your other question, the same code should work, just for SS/SE instead of JS/JE:
var shiftErrEvents = itCompareDay
.Select((ev, rowNum) => new ev.EventType, ev.EventDateTime, rowNum )
.Where(cd => cd.EventType == "SS" || cd.EventType == "SE")
.GroupByWhile((pd, cd) => pd.EventType == "SS" && cd.EventType == "SE" && pd.EventDateTime.Date == cd.EventDateTime.Date)
.Where(cdg => cdg.Count() != 2)
.SelectMany(cdg => cdg.Select(cd => new cd.rowNum, ErrMsg = cd.EventType == "SE" ? "SE without preceding SS" : "SS without following SE" ));
Note: In both cases, a job or shift that crosses the midnight boundary (hence, is on different days) will be handled as two mismatched events.
add a comment |
up vote
0
down vote
Using the same extensions methods from your other question, the same code should work, just for SS/SE instead of JS/JE:
var shiftErrEvents = itCompareDay
.Select((ev, rowNum) => new ev.EventType, ev.EventDateTime, rowNum )
.Where(cd => cd.EventType == "SS" || cd.EventType == "SE")
.GroupByWhile((pd, cd) => pd.EventType == "SS" && cd.EventType == "SE" && pd.EventDateTime.Date == cd.EventDateTime.Date)
.Where(cdg => cdg.Count() != 2)
.SelectMany(cdg => cdg.Select(cd => new cd.rowNum, ErrMsg = cd.EventType == "SE" ? "SE without preceding SS" : "SS without following SE" ));
Note: In both cases, a job or shift that crosses the midnight boundary (hence, is on different days) will be handled as two mismatched events.
add a comment |
up vote
0
down vote
up vote
0
down vote
Using the same extensions methods from your other question, the same code should work, just for SS/SE instead of JS/JE:
var shiftErrEvents = itCompareDay
.Select((ev, rowNum) => new ev.EventType, ev.EventDateTime, rowNum )
.Where(cd => cd.EventType == "SS" || cd.EventType == "SE")
.GroupByWhile((pd, cd) => pd.EventType == "SS" && cd.EventType == "SE" && pd.EventDateTime.Date == cd.EventDateTime.Date)
.Where(cdg => cdg.Count() != 2)
.SelectMany(cdg => cdg.Select(cd => new cd.rowNum, ErrMsg = cd.EventType == "SE" ? "SE without preceding SS" : "SS without following SE" ));
Note: In both cases, a job or shift that crosses the midnight boundary (hence, is on different days) will be handled as two mismatched events.
Using the same extensions methods from your other question, the same code should work, just for SS/SE instead of JS/JE:
var shiftErrEvents = itCompareDay
.Select((ev, rowNum) => new ev.EventType, ev.EventDateTime, rowNum )
.Where(cd => cd.EventType == "SS" || cd.EventType == "SE")
.GroupByWhile((pd, cd) => pd.EventType == "SS" && cd.EventType == "SE" && pd.EventDateTime.Date == cd.EventDateTime.Date)
.Where(cdg => cdg.Count() != 2)
.SelectMany(cdg => cdg.Select(cd => new cd.rowNum, ErrMsg = cd.EventType == "SE" ? "SE without preceding SS" : "SS without following SE" ));
Note: In both cases, a job or shift that crosses the midnight boundary (hence, is on different days) will be handled as two mismatched events.
answered Nov 9 at 19:43
NetMage
12.6k11734
12.6k11734
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53227797%2fiterate-through-linq-to-entities-results-until-condition-is-met%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Can you please provide a simple data example with which the error occurs? Your code readability is... lacking, which is hindering my understanding of the logical succession of operations you expect to have. An MCVE would be allround helpful here.
– Flater
Nov 9 at 15:01
@Hogan My question would be how do I iterate through the days events to find an unpaired SE tag. For example on 11/8/2018 I have 3 shifts and for that day I have 3 instances of the SS tag but only 2 SE tags which means the 3rd shift started but never ended. How do I find the 3rd SS tag to add an error condition to my list and record the row number?
– Doug Farrell
Nov 9 at 15:32
That is the problem you are trying to solve. We don't just write solutions for people here -- we help with questions. In trying to solve that problem did you run into something that did not work as expected. Is there a test case that fails, for example. We can answer a question -- as long as the question isn't -- how do I solve this problem write all the code for me. You have given us some code but no details on the problems you are having. If I have to spend 3 hours to understand the solution to answer your question, I'm not going to. If it takes me 10 mins then I'm happy to help.
– Hogan
Nov 9 at 17:21
You can find a lot of good resources in the help screens about how to ask a good question. In this case I would suggest giving us the input data and the expected output data and what your current code is doing. If you do this I expect someone will answer very quickly. (might even be me)
– Hogan
Nov 9 at 17:23