I've built a program that creates employee schedules and assigns employees to those schedules. I can assign the schedule with no issues, but I'm having problems figuring out how to make that schedule disappear from the dropdown list if it's already been assigned to an employee so that the same schedule isn't given to two employees.
#Html.DropDownListFor(m => m.ApplicationUser.ScheduleSum, new SelectList(Model.ScheduleBuilderList, "ScheduleSummary", "ScheduleSummary"), "Choose a schedule", new {#class = "form-control"}
How can I get a schedule ID to disappear when assigned and reappeared if not assigned?
Let me know if you need any additional information.
Schedule Model
public int Id { get; set; }
public int ScheduleId { get; set; }
public string DaysMatching { get; set; }
public string Days { get; set; }
public string ScheduleSummary { get; set; }
public string MondaySchedule { get; set; }
public string TuesdaySchedule { get; set; }
public string WednesdaySchedule { get; set; }
public string ThursdaySchedule { get; set; }
public string FridaySchedule { get; set; }
public string SaturdaySchedule { get; set; }
public string SundaySchedule { get; set; }
public string MondayStartTime { get; set; }
public string TuesdayStartTime { get; set; }
public string WednesdayStartTime { get; set; }
public string ThursdayStartTime { get; set; }
public string FridayStartTime { get; set; }
public string SaturdayStartTime { get; set; }
public string SundayStartTime { get; set; }
public string MondayEndTime { get; set; }
public string TuesdayEndTime { get; set; }
public string WednesdayEndTime { get; set; }
public string ThursdayEndTime { get; set; }
public string FridayEndTime { get; set; }
public string SaturdayEndTime { get; set; }
public string SundayEndTime { get; set; }
public string MondayFirstBreak { get; set; }
public string TuesdayFirstBreak { get; set; }
public string WednesdayFirstBreak { get; set; }
public string ThursdayFirstBreak { get; set; }
public string FridayFirstBreak { get; set; }
public string SaturdayFirstBreak { get; set; }
public string SundayFirstBreak { get; set; }
public string MondaySecondBreak { get; set; }
public string TuesdaySecondBreak { get; set; }
public string WednesdaySecondBreak { get; set; }
public string ThursdaySecondBreak { get; set; }
public string FridaySecondBreak { get; set; }
public string SaturdaySecondBreak { get; set; }
public string SundaySecondBreak { get; set; }
public string MondayLunch { get; set; }
public string TuesdayLunch { get; set; }
public string WednesdayLunch { get; set; }
public string ThursdayLunch { get; set; }
public string FridayLunch { get; set; }
public string SaturdayLunch { get; set; }
public string SundayLunch { get; set; }
Employee Model
public class ApplicationUser : IdentityUser
{
public int ScheduleId { get; set; }
public ScheduleBuilder ScheduleBuilder { get; set; }
public string ScheduleSum { get; set; }
[Required]
[DataType(DataType.PhoneNumber)]
[RegularExpression(#"^\(?([0-9]{3})\)?[-]([0-9]{3})[-]([0-9]{4})$", ErrorMessage = "Please enter correct format with hyphens. i.e. 407-200-2000")]
[Display(Name = "Phone Number")]
public string PhoneNumbers { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required(ErrorMessage = "You must provide a valid Email")]
public string EmailAccount => Email;
}
Schedule Controller Save ActionResult
[HttpPost]
public ActionResult Save(ScheduleBuilder scheduleBuilder)
{
if (!ModelState.IsValid)
{
var viewModel = new TierViewModel()
{
ScheduleBuilder = scheduleBuilder
};
return View("ScheduleBuilder", viewModel);
}
if (scheduleBuilder.Id == 0)
{
_context.ScheduleBuilder.Add(scheduleBuilder);
if (scheduleBuilder.MondayStartTime != null)
{
scheduleBuilder.MondaySchedule = "Mon";
}
if (scheduleBuilder.TuesdayStartTime != null)
{
scheduleBuilder.TuesdaySchedule = "Tue";
}
if (scheduleBuilder.WednesdayStartTime != null)
{
scheduleBuilder.WednesdaySchedule = "Wed";
}
if (scheduleBuilder.ThursdayStartTime != null)
{
scheduleBuilder.ThursdaySchedule = "Thur";
}
if (scheduleBuilder.FridayStartTime != null)
{
scheduleBuilder.FridaySchedule = "Fri";
}
if (scheduleBuilder.SaturdayStartTime != null)
{
scheduleBuilder.SaturdaySchedule = "Sat";
}
if (scheduleBuilder.SundayStartTime != null)
{
scheduleBuilder.SundaySchedule = "Sun";
}
scheduleBuilder.Days = scheduleBuilder.SundaySchedule + " " +
scheduleBuilder.MondaySchedule + " " +
scheduleBuilder.TuesdaySchedule + " " +
scheduleBuilder.WednesdaySchedule + " " +
scheduleBuilder.ThursdaySchedule + " " +
scheduleBuilder.FridaySchedule + " " +
scheduleBuilder.SaturdaySchedule;
scheduleBuilder.DaysMatching = "Yes";
var startTime = scheduleBuilder.MondayStartTime + "-" + scheduleBuilder.MondayEndTime;
if (scheduleBuilder.MondayStartTime == null)
{
startTime = scheduleBuilder.TuesdayStartTime + "-" + scheduleBuilder.TuesdayEndTime;
}
if (scheduleBuilder.MondayStartTime == null && scheduleBuilder.TuesdayStartTime == null)
{
startTime = scheduleBuilder.WednesdayStartTime + "-" + scheduleBuilder.WednesdayEndTime;
}
if (scheduleBuilder.WednesdayStartTime == null && scheduleBuilder.MondayStartTime == null && scheduleBuilder.TuesdayStartTime == null)
{
startTime = scheduleBuilder.ThursdayStartTime + "-" + scheduleBuilder.ThursdayEndTime; ;
}
if (scheduleBuilder.ThursdayStartTime == null && scheduleBuilder.WednesdayStartTime == null && scheduleBuilder.MondayStartTime == null && scheduleBuilder.TuesdayStartTime == null)
{
startTime = scheduleBuilder.FridayStartTime + "-" + scheduleBuilder.FridayEndTime; ;
}
if (scheduleBuilder.FridayStartTime == null && scheduleBuilder.ThursdayStartTime == null && scheduleBuilder.WednesdayStartTime == null && scheduleBuilder.TuesdayStartTime == null && scheduleBuilder.MondayStartTime == null)
{
startTime = scheduleBuilder.SaturdayStartTime + "-" + scheduleBuilder.SaturdayEndTime; ;
}
if (scheduleBuilder.SaturdayStartTime == null && scheduleBuilder.FridayStartTime == null && scheduleBuilder.ThursdayStartTime == null && scheduleBuilder.WednesdayStartTime == null && scheduleBuilder.TuesdayStartTime == null && scheduleBuilder.MondayStartTime == null)
{
startTime = scheduleBuilder.SundayStartTime + "-" + scheduleBuilder.SundayEndTime; ;
}
System.Diagnostics.Debug.Write("Monday End Time: " + scheduleBuilder.MondayEndTime);
System.Diagnostics.Debug.Write("Sunday End Time: " + scheduleBuilder.SundayEndTime);
if (scheduleBuilder.SundayStartTime != scheduleBuilder.MondayStartTime && scheduleBuilder.MondayStartTime != null && scheduleBuilder.SundayStartTime != null || scheduleBuilder.SundayEndTime != scheduleBuilder.MondayEndTime && scheduleBuilder.MondayEndTime != null && scheduleBuilder.SundayEndTime != null
|| scheduleBuilder.SundayStartTime != scheduleBuilder.TuesdayStartTime && scheduleBuilder.TuesdayStartTime != null && scheduleBuilder.SundayStartTime != null || scheduleBuilder.SundayEndTime != scheduleBuilder.TuesdayEndTime && scheduleBuilder.TuesdayEndTime != null && scheduleBuilder.SundayEndTime != null
|| scheduleBuilder.SundayStartTime != scheduleBuilder.WednesdayStartTime && scheduleBuilder.WednesdayStartTime != null && scheduleBuilder.SundayStartTime != null || scheduleBuilder.SundayEndTime != scheduleBuilder.WednesdayEndTime && scheduleBuilder.WednesdayEndTime != null && scheduleBuilder.SundayEndTime != null
|| scheduleBuilder.SundayStartTime != scheduleBuilder.ThursdayStartTime && scheduleBuilder.ThursdayStartTime != null && scheduleBuilder.SundayStartTime != null || scheduleBuilder.SundayEndTime != scheduleBuilder.ThursdayEndTime && scheduleBuilder.ThursdayEndTime != null && scheduleBuilder.SundayEndTime != null
|| scheduleBuilder.SundayStartTime != scheduleBuilder.FridayStartTime && scheduleBuilder.FridayStartTime != null && scheduleBuilder.SundayStartTime != null || scheduleBuilder.SundayEndTime != scheduleBuilder.FridayEndTime && scheduleBuilder.FridayEndTime != null && scheduleBuilder.SundayEndTime != null
|| scheduleBuilder.SundayStartTime != scheduleBuilder.SaturdayStartTime && scheduleBuilder.SaturdayStartTime != null && scheduleBuilder.SundayStartTime != null || scheduleBuilder.SundayEndTime != scheduleBuilder.SaturdayEndTime && scheduleBuilder.SaturdayEndTime != null && scheduleBuilder.SundayEndTime != null
|| scheduleBuilder.MondayStartTime != scheduleBuilder.TuesdayStartTime && scheduleBuilder.TuesdayStartTime != null && scheduleBuilder.MondayStartTime != null || scheduleBuilder.MondayEndTime != scheduleBuilder.TuesdayEndTime && scheduleBuilder.MondayEndTime != null && scheduleBuilder.TuesdayEndTime != null
|| scheduleBuilder.MondayStartTime != scheduleBuilder.WednesdayStartTime && scheduleBuilder.WednesdayStartTime != null && scheduleBuilder.MondayStartTime != null || scheduleBuilder.MondayEndTime != scheduleBuilder.WednesdayEndTime && scheduleBuilder.MondayEndTime != null && scheduleBuilder.WednesdayEndTime != null
|| scheduleBuilder.MondayStartTime != scheduleBuilder.ThursdayStartTime && scheduleBuilder.ThursdayStartTime != null && scheduleBuilder.MondayStartTime != null || scheduleBuilder.MondayEndTime != scheduleBuilder.ThursdayEndTime && scheduleBuilder.MondayEndTime != null && scheduleBuilder.ThursdayEndTime != null
|| scheduleBuilder.MondayStartTime != scheduleBuilder.FridayStartTime && scheduleBuilder.FridayStartTime != null && scheduleBuilder.MondayStartTime != null || scheduleBuilder.MondayEndTime != scheduleBuilder.FridayEndTime && scheduleBuilder.MondayEndTime != null && scheduleBuilder.FridayEndTime != null
|| scheduleBuilder.MondayStartTime != scheduleBuilder.SaturdayStartTime && scheduleBuilder.SaturdayStartTime != null && scheduleBuilder.MondayStartTime != null || scheduleBuilder.MondayEndTime != scheduleBuilder.SaturdayEndTime && scheduleBuilder.MondayEndTime != null && scheduleBuilder.SaturdayEndTime != null
|| scheduleBuilder.TuesdayStartTime != scheduleBuilder.WednesdayStartTime && scheduleBuilder.WednesdayStartTime != null && scheduleBuilder.TuesdayStartTime != null || scheduleBuilder.TuesdayEndTime != scheduleBuilder.WednesdayEndTime && scheduleBuilder.WednesdayEndTime != null && scheduleBuilder.TuesdayEndTime != null
|| scheduleBuilder.TuesdayStartTime != scheduleBuilder.ThursdayStartTime && scheduleBuilder.ThursdayStartTime != null && scheduleBuilder.TuesdayStartTime != null || scheduleBuilder.TuesdayEndTime != scheduleBuilder.ThursdayEndTime && scheduleBuilder.ThursdayEndTime != null && scheduleBuilder.TuesdayEndTime != null
|| scheduleBuilder.TuesdayStartTime != scheduleBuilder.FridayStartTime && scheduleBuilder.FridayStartTime != null && scheduleBuilder.TuesdayStartTime != null || scheduleBuilder.TuesdayEndTime != scheduleBuilder.FridayEndTime && scheduleBuilder.FridayEndTime != null && scheduleBuilder.TuesdayEndTime != null
|| scheduleBuilder.TuesdayStartTime != scheduleBuilder.SaturdayStartTime && scheduleBuilder.SaturdayStartTime != null && scheduleBuilder.TuesdayStartTime != null || scheduleBuilder.TuesdayEndTime != scheduleBuilder.SaturdayEndTime && scheduleBuilder.SaturdayEndTime != null && scheduleBuilder.TuesdayEndTime != null
|| scheduleBuilder.WednesdayStartTime != scheduleBuilder.ThursdayStartTime && scheduleBuilder.ThursdayStartTime != null && scheduleBuilder.WednesdayStartTime != null || scheduleBuilder.WednesdayEndTime != scheduleBuilder.ThursdayEndTime && scheduleBuilder.ThursdayEndTime != null && scheduleBuilder.WednesdayEndTime != null
|| scheduleBuilder.WednesdayStartTime != scheduleBuilder.FridayStartTime && scheduleBuilder.FridayStartTime != null && scheduleBuilder.WednesdayStartTime != null || scheduleBuilder.WednesdayEndTime != scheduleBuilder.FridayEndTime && scheduleBuilder.FridayEndTime != null && scheduleBuilder.WednesdayEndTime != null
|| scheduleBuilder.WednesdayStartTime != scheduleBuilder.SaturdayStartTime && scheduleBuilder.SaturdayStartTime != null && scheduleBuilder.WednesdayStartTime != null || scheduleBuilder.WednesdayEndTime != scheduleBuilder.SaturdayEndTime && scheduleBuilder.SaturdayEndTime != null && scheduleBuilder.WednesdayEndTime != null
|| scheduleBuilder.ThursdayStartTime != scheduleBuilder.FridayStartTime && scheduleBuilder.ThursdayStartTime != null && scheduleBuilder.FridayStartTime != null || scheduleBuilder.ThursdayEndTime != scheduleBuilder.FridayEndTime && scheduleBuilder.ThursdayEndTime != null && scheduleBuilder.FridayEndTime != null
|| scheduleBuilder.ThursdayStartTime != scheduleBuilder.SaturdayStartTime && scheduleBuilder.ThursdayStartTime != null && scheduleBuilder.SaturdayStartTime != null || scheduleBuilder.ThursdayEndTime != scheduleBuilder.SaturdayEndTime && scheduleBuilder.ThursdayEndTime != null && scheduleBuilder.SaturdayEndTime != null
|| scheduleBuilder.FridayStartTime != scheduleBuilder.SaturdayStartTime && scheduleBuilder.FridayStartTime != null && scheduleBuilder.SaturdayStartTime != null || scheduleBuilder.FridayEndTime != scheduleBuilder.SaturdayEndTime && scheduleBuilder.FridayEndTime != null && scheduleBuilder.SaturdayEndTime != null
)
{
System.Diagnostics.Debug.Write("Tuesday and Sunday Don't Match!");
scheduleBuilder.DaysMatching = "No";
}
scheduleBuilder.ScheduleSummary = scheduleBuilder.Days + " " + startTime + " | Scheduled Days Match: " + scheduleBuilder.DaysMatching;
}
else
{
var scheduleInDb = _context.ScheduleBuilder.Single(a => a.Id == scheduleBuilder.Id);
scheduleInDb.MondayStartTime = scheduleBuilder.MondayStartTime;
scheduleInDb.TuesdayStartTime = scheduleBuilder.TuesdayStartTime;
scheduleInDb.WednesdayStartTime = scheduleBuilder.WednesdayStartTime;
scheduleInDb.ThursdayStartTime = scheduleBuilder.ThursdayStartTime;
scheduleInDb.FridayStartTime = scheduleBuilder.FridayStartTime;
scheduleInDb.SaturdayStartTime = scheduleBuilder.SaturdayStartTime;
scheduleInDb.SundayStartTime = scheduleBuilder.SundayStartTime;
scheduleInDb.MondayEndTime = scheduleBuilder.MondayEndTime;
scheduleInDb.TuesdayEndTime = scheduleBuilder.TuesdayEndTime;
scheduleInDb.WednesdayEndTime = scheduleBuilder.WednesdayEndTime;
scheduleInDb.ThursdayEndTime = scheduleBuilder.ThursdayEndTime;
scheduleInDb.FridayEndTime = scheduleBuilder.FridayEndTime;
scheduleInDb.SaturdayEndTime = scheduleBuilder.SaturdayEndTime;
scheduleInDb.SundayEndTime = scheduleBuilder.SundayEndTime;
scheduleInDb.MondayFirstBreak = scheduleBuilder.MondayFirstBreak;
scheduleInDb.TuesdayFirstBreak = scheduleBuilder.TuesdayFirstBreak;
scheduleInDb.WednesdayFirstBreak = scheduleBuilder.WednesdayFirstBreak;
scheduleInDb.ThursdayFirstBreak = scheduleBuilder.ThursdayFirstBreak;
scheduleInDb.FridayFirstBreak = scheduleBuilder.FridayFirstBreak;
scheduleInDb.SaturdayFirstBreak = scheduleBuilder.SaturdayFirstBreak;
scheduleInDb.SundayFirstBreak = scheduleBuilder.SundayFirstBreak;
scheduleInDb.MondaySecondBreak = scheduleBuilder.MondaySecondBreak;
scheduleInDb.TuesdaySecondBreak = scheduleBuilder.TuesdaySecondBreak;
scheduleInDb.WednesdaySecondBreak = scheduleBuilder.WednesdaySecondBreak;
scheduleInDb.ThursdaySecondBreak = scheduleBuilder.ThursdaySecondBreak;
scheduleInDb.FridaySecondBreak = scheduleBuilder.FridayFirstBreak;
scheduleInDb.SaturdaySecondBreak = scheduleBuilder.SaturdaySecondBreak;
scheduleInDb.SundaySecondBreak = scheduleBuilder.SundaySecondBreak;
scheduleInDb.MondayLunch = scheduleBuilder.MondayLunch;
scheduleInDb.TuesdayLunch = scheduleBuilder.TuesdayLunch;
scheduleInDb.WednesdayLunch = scheduleBuilder.WednesdayLunch;
scheduleInDb.ThursdayLunch = scheduleBuilder.ThursdayLunch;
scheduleInDb.FridayLunch = scheduleBuilder.FridayLunch;
scheduleInDb.SaturdayLunch = scheduleBuilder.SaturdayLunch;
scheduleInDb.SundayLunch = scheduleBuilder.SundayLunch;
}
_context.SaveChanges();
return RedirectToAction("ScheduleBuilder", "Schedule");
}
ViewModel
public class TierViewModel
{
public IEnumerable<Tier> TierLevel { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public IEnumerable<ShiftLevel> ShiftLevel { get; set; }
public ScheduleBuilder ScheduleBuilder { get; set; }
public IEnumerable<ScheduleBuilder> ScheduleBuilderList { get; set; }
public ScheduleBuilderTier2 ScheduleBuilderTwo { get; set; }
}
Related
I have List<Book> Books
public class Book
{
public string Name { get; set; }
public string Author { get; set; }
public string Category { get; set; }
public string Language { get; set; }
public DateTime Publication_date { get; set; }
public string ISBN { get; set; }
}
tried this, but it only work if all parameters matched
foreach (var item in Books)
{
if ((!string.IsNullOrEmpty(name) && item.Name == name)
&& (!string.IsNullOrEmpty(author) && item.Author == author)
&& (!string.IsNullOrEmpty(category) && item.Category == category)
&& (!string.IsNullOrEmpty(language) && item.Language == language)
&& (Publication_date.HasValue && item.Publication_date == Publication_date)
&& (!string.IsNullOrEmpty(ISBN) && item.ISBN == ISBN))
{
Console.WriteLine(item);
}
}
how to make filter by one or more parameters? like if i enter only "name" and "Language" it will print me the book where both parameters a matched (if string is null or empty then parameter should be skipped)
Thanks)
The logic is a touch complicated, but made simpler by use of continue.
foreach (var item in Books)
{
if (!string.IsNullOrEmpty(name) && item.Name != name) continue;
if (!string.IsNullOrEmpty(author) && item.Author != author) continue;
if (!string.IsNullOrEmpty(category) && item.Category != category) continue;
if (!string.IsNullOrEmpty(language) && item.Language != language) continue;
if (Publication_date.HasValue && item.Publication_Date != Publication_Date) continue;
if (!string.IsNullOrEmpty(ISBN) && item.ISBN!= ISBN) continue;
Console.WriteLine(item);
}
you want it in a form where each condition returns true if null, or true if match. Basically null/empty means anything is a match
like
(string.IsNullOrEmpty(name) || item.Name == name)
then && them altogether
foreach (var item in Books)
{
if ((string.IsNullOrEmpty(name) || item.Name == name)
&& (string.IsNullOrEmpty(author) || item.Author == author)
&& (string.IsNullOrEmpty(category) || item.Category == category)
&& (string.IsNullOrEmpty(language) || item.Language == language)
&& (!Publication_date.HasValue || item.Publication_date == Publication_date)
&& (string.IsNullOrEmpty(ISBN) || item.ISBN == ISBN))
{
Console.WriteLine(item);
}
}
to be a little clearer you can do
bool IsMatchForFilter(string filter, string value)
=> string.IsNullOrEmpty(filter) || filter == value;
then do
if(
IsMatchForFilter(name, item.Name)
&& IsMatchForFilter(author, item.Author)
...
Try it like this, use the OR (||) operator
foreach (var item in Books)
{
if ((!string.IsNullOrEmpty(name) && item.Name == name)
|| (!string.IsNullOrEmpty(author) && item.Author == author)
|| (!string.IsNullOrEmpty(category) && item.Category == category)
|| (!string.IsNullOrEmpty(language) && item.Language == language)
|| (Publication_date.HasValue && item.Publication_date == Publication_date)
|| (!string.IsNullOrEmpty(ISBN) && item.ISBN == ISBN))
{
Console.WriteLine(item);
}
}
I have a large amount of directory type data, with properties like email, telephone, post code etc. I want to detect and group duplicates but on all the multiple properties, where groups can be made up of duplicates on different properties but not all have to be direct duplicates of each other but may be related via another row.
Example data:
Name | Email | Tel | Postcode
John Sim | j#j.com | 111 | C67
J Sim | | 111 |
John S | | | C67
I'd like to be able to find a way that I can detect duplicates over the Email, Tel and Postcode properties but even if they don't all match. So in the example above I'd get all 3 of the above grouped, even though the last one has no directed match to the middle one, but the first shares a match with both.
If that makes sense! obviously that is very simplified, I have a 100's of records I'm trying to group in a way I can display the duplicate groups.
So far I have found some very inefficient ways of doing this, involving going through each row, grouping any direct duplicates, but then checking to see if any of those duplicates also have duplicates and if so move them all together into a new group. But I'm trying to find some inspiration and a more efficient way of doing it :)
Thanks!
You could get the complexity down to O(n) using indexes in a foreach loop and combining on every iteration like the following:
foreach (var entry in list)
{
Group emailGroup = null;
Group telGroup = null;
Group postcodeGroup = null;
if (entry.Email != null && _emailGroups.TryGetValue(entry.Email, out emailGroup))
if (!emailGroup.Add(entry)) emailGroup = null;
if (entry.Tel != null && _telGroups.TryGetValue(entry.Tel, out telGroup))
if (!telGroup.Add(entry)) telGroup = null;
if (entry.Postcode != null && _postcodeGroups.TryGetValue(entry.Postcode, out postcodeGroup))
if (!postcodeGroup.Add(entry)) postcodeGroup = null;
if (emailGroup == null && telGroup == null && postcodeGroup == null)
{
CreateGroup(entry);
continue;
}
CombineGroups(emailGroup, telGroup, postcodeGroup);
}
Of course, you would have to decide + handle what you want to do on collision etc. and if need be, add any Name logic (e.g. Split First Name + Middle + Last) and then do a 2 way Contains on each (quite expensive so might want to look at string indexes)
Full Code + Test
See Method
[Test]
public void Test()
in the following.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentAssertions;
using NUnit.Framework;
namespace StackOverflow
{
[TestFixture]
public class Class1
{
private Dictionary<string, Group> _emailGroups;
private Dictionary<string, Group> _telGroups;
private Dictionary<string, Group> _postcodeGroups;
private void CreateGroup(Entry entry)
{
var group = new Group(entry);
if (group.Email != null && !_emailGroups.ContainsKey(group.Email))
_emailGroups[group.Email] = group;
if (group.Tel != null && !_emailGroups.ContainsKey(group.Tel))
_telGroups[group.Tel] = group;
if (group.PostCode != null && !_emailGroups.ContainsKey(group.PostCode))
_postcodeGroups[group.PostCode] = group;
}
private void CombineGroups(Group emailGroup, Group telGroup, Group postcodeGroup)
{
if (emailGroup != telGroup && emailGroup != null && telGroup != null)
{
if (emailGroup.CanCombine(telGroup))
{
emailGroup.Add(telGroup);
UpdateIndexes(emailGroup, telGroup);
telGroup = null;
}
;
}
if (emailGroup != postcodeGroup && emailGroup != null && postcodeGroup != null)
{
if (emailGroup.CanCombine(postcodeGroup))
{
emailGroup.Add(postcodeGroup);
UpdateIndexes(emailGroup, postcodeGroup);
postcodeGroup = null;
}
;
}
if (telGroup != postcodeGroup && telGroup != null && postcodeGroup != null)
{
if (telGroup.CanCombine(postcodeGroup))
{
telGroup.Add(postcodeGroup);
UpdateIndexes(telGroup, postcodeGroup);
postcodeGroup = null;
}
;
}
}
private void UpdateIndexes(Group newGroup, Group oldGroup)
{
Group group;
if (oldGroup.Email != null
&& _emailGroups.TryGetValue(oldGroup.Email, out group)
&& group == oldGroup)
_emailGroups[oldGroup.Email] = newGroup;
if (oldGroup.Tel != null
&& _telGroups.TryGetValue(oldGroup.Tel, out group)
&& group == oldGroup)
_telGroups[oldGroup.Tel] = newGroup;
if (oldGroup.PostCode != null
&& _postcodeGroups.TryGetValue(oldGroup.PostCode, out group)
&& group == oldGroup)
_postcodeGroups[oldGroup.PostCode] = newGroup;
}
public class Group
{
public HashSet<Entry> Entries = new HashSet<Entry>();
public Group(Entry entry)
{
Email = entry.Email;
Tel = entry.Tel;
PostCode = entry.PostCode;
Entries.Add(entry);
}
public string Email { get; set; }
public string Tel { get; set; }
public string PostCode { get; set; }
public bool Matches(Entry entry)
{
if (Email != null && entry.Email != null && entry.Email != Email)
return false;
if (Tel != null && entry.Tel != null && entry.Tel != Tel)
return false;
if (PostCode != null && entry.PostCode != null && entry.PostCode != PostCode)
return false;
return true;
}
public bool Add(Entry entry)
{
if (!Matches(entry))
return false;
Entries.Add(entry);
if (Email == null && entry.Email != null)
Email = entry.Email;
if (Tel == null && entry.Tel != null)
Tel = entry.Tel;
if (PostCode == null && entry.PostCode != null)
PostCode = entry.PostCode;
return true;
}
public bool CanCombine(Group entry)
{
if (Email != null && entry.Email != null && Email != entry.Email)
return false;
if (Tel != null && entry.Tel != null && Tel != entry.Tel)
return false;
if (PostCode != null && entry.PostCode != null && PostCode != entry.PostCode)
return false;
return true;
}
public void Add(Group group)
{
foreach (var entry in group.Entries)
{
Add(entry);
}
}
public override string ToString()
{
var sb = new StringBuilder();
sb.AppendLine($"Key: {Email ?? "null"} | {Tel ?? "null"} | {PostCode ?? "null"}");
foreach (var entry in Entries)
{
sb.AppendLine(entry.ToString());
}
return sb.ToString();
}
}
public class Entry
{
public Entry(string name, string email, string tel, string postCode)
{
Name = name;
Email = email;
Tel = tel;
PostCode = postCode;
}
public string Name { get; set; }
public string Email { get; set; }
public string Tel { get; set; }
public string PostCode { get; set; }
public override string ToString()
{
return $"Entry: {Name ?? "null"} | {Email ?? "null"} | {Tel ?? "null"} | {PostCode ?? "null"}";
}
}
[Test]
public void Test()
{
var list = new List<Entry>
{
new Entry("John S", null, null, "C67"),
new Entry("J Sim", null, "111", null),
new Entry("John Sim", "j#j.com", "111", "C67")
};
_emailGroups = new Dictionary<string, Group>();
_telGroups = new Dictionary<string, Group>();
_postcodeGroups = new Dictionary<string, Group>();
foreach (var entry in list)
{
Group emailGroup = null;
Group telGroup = null;
Group postcodeGroup = null;
if (entry.Email != null && _emailGroups.TryGetValue(entry.Email, out emailGroup))
if (!emailGroup.Add(entry)) emailGroup = null;
if (entry.Tel != null && _telGroups.TryGetValue(entry.Tel, out telGroup))
if (!telGroup.Add(entry)) telGroup = null;
if (entry.PostCode != null && _postcodeGroups.TryGetValue(entry.PostCode, out postcodeGroup))
if (!postcodeGroup.Add(entry)) postcodeGroup = null;
if (emailGroup == null && telGroup == null && postcodeGroup == null)
{
CreateGroup(entry);
continue;
}
CombineGroups(emailGroup, telGroup, postcodeGroup);
}
var groups = _emailGroups.Select(x => x.Value)
.Union(_telGroups.Select(x => x.Value))
.Union(_postcodeGroups.Select(x => x.Value))
.Distinct()
.ToList();
foreach (var grp in groups)
{
Console.WriteLine(grp.ToString());
}
groups.Should().HaveCount(1);
groups.First().Entries.Should().HaveCount(3);
}
}
}
I am trying to create a multilevel tree view using ObjectListview. But problem is that it create only one parent and add the child in that parent.
My tree stricture in any form
A
-a1
-a2
-a3
-a4
- a4(i)
B
- b (a)
- c (a)
C
Means my tree can be multilevel. Below is my code that i use to bind the treeListView
private void InitializeData(int appId)
{
try
{
UserDAC _UserDAC = new UserDAC();
DataTable SEC_InfoTreeView = _UserDAC.GetAPPITEMSbyAPPID(appId, "", this.FindForm().Name, "InitializeFristTabValue()").Tables[0];
data = new List<Node> { }; // this i bind my treelistview
var Blank = new Node(0, "User Name", "Status", "Static Caption");
var parent1= new Node(0,"", "", " ");
treeListView1.SmallImageList = imageList1;
if (SEC_InfoTreeView.Rows.Count > 0)
{
for (int i = 0; i < SEC_InfoTreeView.Rows.Count; i++)
{
if (i == 0)
data.Add(Blank);
int PrentID = Convert.ToInt32(SEC_InfoTreeView.Rows[i]["PARENTID"]);
if (Convert.ToString(SEC_InfoTreeView.Rows[i]["INDENT"]) != "")
{
if (PrentID == 0 && Convert.ToInt32(SEC_InfoTreeView.Rows[i]["INDENT"]) == 0)
{
data.Add(parent1);
var parentAdd = new Node(Convert.ToInt32(SEC_InfoTreeView.Rows[i]["APPITEMID"]), Convert.ToString(SEC_InfoTreeView.Rows[i]["USERNAME"]), "", Convert.ToString(SEC_InfoTreeView.Rows[i]["ACAPTION"]));
parent1 = parentAdd;
}
else if (PrentID != 0 && Convert.ToInt32(SEC_InfoTreeView.Rows[i]["INDENT"]) == 2)
{
parent1.Children.Add(new Node(Convert.ToInt32(SEC_InfoTreeView.Rows[i]["APPITEMID"]),Convert.ToString(SEC_InfoTreeView.Rows[i]["USERNAME"]), "", Convert.ToString(SEC_InfoTreeView.Rows[i]["ACAPTION"])));
}
else if (PrentID != 0 && Convert.ToInt32(SEC_InfoTreeView.Rows[i]["INDENT"]) == 4)
{
parent1.Children.Add(new Node(Convert.ToInt32(SEC_InfoTreeView.Rows[i]["APPITEMID"]),Convert.ToString(SEC_InfoTreeView.Rows[i]["USERNAME"]), "", Convert.ToString(SEC_InfoTreeView.Rows[i]["ACAPTION"])));
}
else if (PrentID != 0 && Convert.ToInt32(SEC_InfoTreeView.Rows[i]["INDENT"]) == 6)
{
parent1.Children.Add(new Node(Convert.ToInt32(SEC_InfoTreeView.Rows[i]["APPITEMID"]),Convert.ToString(SEC_InfoTreeView.Rows[i]["USERNAME"]),"", Convert.ToString(SEC_InfoTreeView.Rows[i]["ACAPTION"])));
}
else if (PrentID != 0 && Convert.ToInt32(SEC_InfoTreeView.Rows[i]["INDENT"]) == 10)
{
parent1.Children.Add(new Node(Convert.ToInt32(SEC_InfoTreeView.Rows[i]["APPITEMID"]),Convert.ToString(SEC_InfoTreeView.Rows[i]["USERNAME"]), "", Convert.ToString(SEC_InfoTreeView.Rows[i]["ACAPTION"])));
}
else if (PrentID != 0 && Convert.ToInt32(SEC_InfoTreeView.Rows[i]["INDENT"]) == 8)
{
parent1.Children.Add(new Node(Convert.ToInt32(SEC_InfoTreeView.Rows[i]["APPITEMID"]),Convert.ToString(SEC_InfoTreeView.Rows[i]["USERNAME"]),"", Convert.ToString(SEC_InfoTreeView.Rows[i]["ACAPTION"])));
}
else if (PrentID != 0 && Convert.ToInt32(SEC_InfoTreeView.Rows[i]["INDENT"]) == 12)
{
parent1.Children.Add(new Node(Convert.ToInt32(SEC_InfoTreeView.Rows[i]["APPITEMID"]),Convert.ToString(SEC_InfoTreeView.Rows[i]["USERNAME"]), "", Convert.ToString(SEC_InfoTreeView.Rows[i]["ACAPTION"])));
}
else if (PrentID != 0 && Convert.ToInt32(SEC_InfoTreeView.Rows[i]["INDENT"]) == 14)
{
parent1.Children.Add(new Node(Convert.ToInt32(SEC_InfoTreeView.Rows[i]["APPITEMID"]),Convert.ToString(SEC_InfoTreeView.Rows[i]["USERNAME"]), "", Convert.ToString(SEC_InfoTreeView.Rows[i]["ACAPTION"])));
}
else if (PrentID != 0 && Convert.ToInt32(SEC_InfoTreeView.Rows[i]["INDENT"]) == 16)
{
parent1.Children.Add(new Node(Convert.ToInt32(SEC_InfoTreeView.Rows[i]["APPITEMID"]),Convert.ToString(SEC_InfoTreeView.Rows[i]["USERNAME"]), "", Convert.ToString(SEC_InfoTreeView.Rows[i]["ACAPTION"])));
}
}
}
}
}
catch (Exception ex)
{
}
}
class Node
{
public int Id { get; private set; }
public string Name { get; private set; }
public string Column1 { get; private set; }
public string Column2 { get; private set; }
public List<Node> Children { get; private set; }
public Node(int id, string name, string col1, string col2)
{
this.Id = id;
this.Name = name;
this.Column1 = col1;
this.Column2 = col2;
this.Children = new List<Node>();
}
}
And for bind the treeview i use this code this.treeListView1.Roots = data; Almost i try this from last 21 hrs but not get the correct treeview structure . Very thanks for your help .
I am having an issue getting the value of a property to commit to the database. I have logic in my get statement that returns a value based on the value's from two other properties. The logic works correctly and displays the correct values in My Web Page, however the value for Rating Field is always set as Null in the database. My code below - if someone can point me in the right direction it would be greatly appreciated.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace PSA.Models
{
public class Issue
{
public virtual int IssueID { get; set; }
public virtual int ProbableClassificationId { get; set; }
public virtual ProbableClassification ProbableClassification { get; set; }
public virtual int SeverityClassificationId { get; set; }
public virtual SeverityClassification SeverityClassification { get; set; }
string ratingstatus;
public virtual string Rating
{
get
{
if (ProbableClassification != null && SeverityClassification != null)
{
if (SeverityClassification.Severity == "Minor")
{
ratingstatus = "Low";
}
if (ProbableClassification.Probability == "Not Likely" && SeverityClassification.Severity == "Medium")
{
ratingstatus = "Low";
}
if (ProbableClassification.Probability == "Likely" && SeverityClassification.Severity == "Medium")
{
ratingstatus = "Medium";
}
if (ProbableClassification.Probability == "Definitely" && SeverityClassification.Severity == "Medium")
{
ratingstatus = "Medium";
}
if (ProbableClassification.Probability == "Not Likely" && SeverityClassification.Severity == "Major")
{
ratingstatus = "Low";
}
if (ProbableClassification.Probability == "Likely" && SeverityClassification.Severity == "Major")
{
ratingstatus = "High";
}
if (ProbableClassification.Probability == "Definitely" && SeverityClassification.Severity == "Major")
{
ratingstatus = "Crtical";
}
}
return ratingstatus;
}
set
{
ratingstatus = value;
}
}
}
}
I have the following code:
public void SetUser(User user)
{
string streetNumber = "";
if (user.Address.StreetNo != null)
streetNumber = user.Address.StreetNo.ToString();
else
streetNumber = "";
}
I get the ever popular
Object reference not set to an instance of an object.
issue.
public void SetUser(User user)
{
string streetNumber = "";
if (user != null && user.Address != null && user.Address.StreetNo != null) {
streetNumber = user.Address.StreetNo.ToString();
}
}
Taking into account #CKoenig's suggestion, the following throws an exception if the user or user.Address are null:
public void SetUser(User user)
{
if (user == null) {
throw new System.ArgumentNullException("user", "user cannot be null");
}
if (user.Address == null) {
throw new System.ArgumentNullException("Address", "Address cannot be null");
}
string streetNumber = "";
if (user.Address.StreetNo != null) {
streetNumber = user.Address.StreetNo.ToString();
}
}
public void SetUser(User user)
{
string streetNumber = String.Empty;
if (user!= null
&& user.Address != null
&& user.Address.StreetNo != null)
{
streetNumber = user.Address.StreetNo.ToString();
}
}
Either user is null, or user.Address is null. You need to test them too.
Check your stacktrace and:
user
user.Address
user.Address.StreetNo
with an if ... == null then ...
if (user != null
&& user.Address != null
&& user.Address.StreetNo != null)
{
// ...
}