I am developing a functionality that adds or removes roles to certain user(s).
I have a problem converting this SQL code into LINQ.
I already tested the SQL and it returns the correct results on SQL Server Management Studio.
Since I am a beginner with LINQ, I find it hard to convert my SQL statements.
/* This sql code works well - returns the users which DO NOT have the role yet in the given company. */
SELECT *
FROM SP.AppUser au
left join
(
SELECT distinct auc.AppUserID
FROM SP.AppUserCompany auc
inner join SP.AppUserCompanyRole aucr ON aucr.AppUserCompanyID = auc.AppUserCompanyID
where auc.CompanyID = 2 and AppRoleID = 4
) AS exist ON exist.AppUserID = au.AppUserID
Where au.AppUserID IN(2088, 38, 1926, 2059, 2058, 1925, 2097, 2061, 2072, 2064)
and exist.AppUserID is null
// Sample code in C# to represent the entities:
AppUser stanley = new AppUser() { AppUserID = 1, FirstName = "Stanley" };
AppUser jerson = new AppUser() { AppUserID = 2, FirstName = "Jerson" };
AppUser philip = new AppUser() { AppUserID = 3, FirstName = "Philip" };
AppUser samantha = new AppUser() { AppUserID = 4, FirstName = "Samantha" };
AppUserCompany auc1 = new AppUserCompany() { AppUserCompanyID = 1, CompanyID = 1, AppUser = stanley };
AppUserCompany auc2 = new AppUserCompany() { AppUserCompanyID = 2, CompanyID = 2, AppUser = stanley };
AppUserCompany auc3 = new AppUserCompany() { AppUserCompanyID = 3, CompanyID = 1, AppUser = jerson };
AppUserCompany auc4 = new AppUserCompany() { AppUserCompanyID = 4, CompanyID = 1, AppUser = philip };
AppUserCompany auc5 = new AppUserCompany() { AppUserCompanyID = 5, CompanyID = 2, AppUser = jerson };
AppUserCompany auc6 = new AppUserCompany() { AppUserCompanyID = 6, CompanyID = 3, AppUser = stanley };
AppUserCompanyRole aucr1 = new AppUserCompanyRole() { AppUserCompanyRoleID = 1, AppUserCompany = auc1, AppRoleID = 1 };
AppUserCompanyRole aucr2 = new AppUserCompanyRole() { AppUserCompanyRoleID = 2, AppUserCompany = auc2, AppRoleID = 1 };
AppUserCompanyRole aucr3 = new AppUserCompanyRole() { AppUserCompanyRoleID = 3, AppUserCompany = auc3, AppRoleID = 1 };
AppUserCompanyRole aucr4 = new AppUserCompanyRole() { AppUserCompanyRoleID = 4, AppUserCompany = auc4, AppRoleID = 1 };
AppUserCompanyRole aucr5 = new AppUserCompanyRole() { AppUserCompanyRoleID = 5, AppUserCompany = auc5, AppRoleID = 1 };
AppUserCompanyRole aucr6 = new AppUserCompanyRole() { AppUserCompanyRoleID = 6, AppUserCompany = auc6, AppRoleID = 1 };
List<AppUser> users = new List<AppUser> { stanley, jerson, philip, samantha };
List<AppUserCompany> appUserCompanies = new List<AppUserCompany> { auc1, auc2, auc3, auc4, auc5, auc6 };
List<AppUserCompanyRole> appUserCompanyRoles = new List<AppUserCompanyRole> { aucr1, aucr2, aucr3, aucr4, aucr5, aucr6 };
// Here is the LINQ code that I tried to get the users "who already have role" in a particular company
var ids = new List<int> { 2088, 38, 1926, 2059, 2058, 1925, 2097, 2061, 2072, 2064 };
var usersToDeleteRole = (from user in appUserDataBinding.Model
where ids.Contains(user.AppUserID)
join auc in appUserCompanyDataBinding.Model on user.AppUserID equals auc.AppUserID into groupJoin1
from subAuc in
(from auc in groupJoin1
join aucr in appUserCompanyRoleDataBinding.Model on auc.AppUserCompanyID equals aucr.AppUserCompanyID
where auc.CompanyID == 2 && aucr.AppRoleID == 4
select auc)
orderby user.LastName
select user).ToList();
My expected result will be a LINQ statement that returns the list of users which who DO NOT have the role yet in a particular given company. The SQL statement I had above worked on SSMS but I cannot make it in LINQ in C# - it means that I need a "negated" version of my LINQ code above.
Thank you for the help.
Cheers.
You can use let statement for you sub queries
public class AppUser
{
public int AppUserID { get; set; }
public string FirstName { get; set; }
}
public class AppUserCompany
{
public int AppUserCompanyID { get; set; }
public int CompanyID { get; set; }
public AppUser AppUser { get; set; }
}
public class AppUserCompanyRole
{
public int AppUserCompanyRoleID { get; set; }
public int AppRoleID { get; set; }
public AppUserCompany AppUserCompany { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Sample code in C# to represent the entities:
AppUser stanley = new AppUser() { AppUserID = 1, FirstName = "Stanley" };
AppUser jerson = new AppUser() { AppUserID = 2, FirstName = "Jerson" };
AppUser philip = new AppUser() { AppUserID = 3, FirstName = "Philip" };
AppUser samantha = new AppUser() { AppUserID = 4, FirstName = "Samantha" };
AppUserCompany auc1 = new AppUserCompany() { AppUserCompanyID = 1, CompanyID = 1, AppUser = stanley };
AppUserCompany auc2 = new AppUserCompany() { AppUserCompanyID = 2, CompanyID = 2, AppUser = stanley };
AppUserCompany auc3 = new AppUserCompany() { AppUserCompanyID = 3, CompanyID = 1, AppUser = jerson };
AppUserCompany auc4 = new AppUserCompany() { AppUserCompanyID = 4, CompanyID = 1, AppUser = philip };
AppUserCompany auc5 = new AppUserCompany() { AppUserCompanyID = 5, CompanyID = 2, AppUser = jerson };
AppUserCompany auc6 = new AppUserCompany() { AppUserCompanyID = 6, CompanyID = 3, AppUser = stanley };
AppUserCompanyRole aucr1 = new AppUserCompanyRole() { AppUserCompanyRoleID = 1, AppUserCompany = auc1, AppRoleID = 1 };
AppUserCompanyRole aucr2 = new AppUserCompanyRole() { AppUserCompanyRoleID = 2, AppUserCompany = auc2, AppRoleID = 1 };
AppUserCompanyRole aucr3 = new AppUserCompanyRole() { AppUserCompanyRoleID = 3, AppUserCompany = auc3, AppRoleID = 1 };
AppUserCompanyRole aucr4 = new AppUserCompanyRole() { AppUserCompanyRoleID = 4, AppUserCompany = auc4, AppRoleID = 1 };
AppUserCompanyRole aucr5 = new AppUserCompanyRole() { AppUserCompanyRoleID = 5, AppUserCompany = auc5, AppRoleID = 1 };
AppUserCompanyRole aucr6 = new AppUserCompanyRole() { AppUserCompanyRoleID = 6, AppUserCompany = auc6, AppRoleID = 1 };
List<AppUser> users = new List<AppUser> { stanley, jerson, philip, samantha };
List<AppUserCompany> appUserCompanies = new List<AppUserCompany> { auc1, auc2, auc3, auc4, auc5, auc6 };
List<AppUserCompanyRole> appUserCompanyRoles = new List<AppUserCompanyRole> { aucr1, aucr2, aucr3, aucr4, aucr5, aucr6 };
var ids = new List<int> { 2088, 38, 1926, 2059, 2058, 1925, 2097, 2061, 2072, 2064 };
var result = from au in users
let appUserIDs = (from auc in appUserCompanies
join aucr in appUserCompanyRoles on auc.AppUserCompanyID equals aucr.AppUserCompany.AppUserCompanyID
where auc.CompanyID == 2 && aucr.AppRoleID == 4 && auc.AppUser.AppUserID == au.AppUserID
select auc.AppUser.AppUserID).Distinct()
where appUserIDs.Count() == 0 && ids.Contains(au.AppUserID)
select au;
Related
I am using Code first migration approach and having problem with populating data.
Database/tables are being created on command update-database but data is not being inserted.
Here is code
internal sealed class Configuration : DbMigrationsConfiguration<webapp_sample.Models.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
//ContextKey = "webapp_sample.Models.ApplicationDbContext";
}
protected override void Seed(webapp_sample.Models.ApplicationDbContext context)
{
context.Companies.AddOrUpdate(p => p.Name,
new Models.Company { CompanyId = 1, Name = "ABC Traders" },
new Models.Company { CompanyId = 2, Name = "XYZ Traders" }
);
context.Divisions.AddOrUpdate(p => p.Name,
new Models.Division { CompanyId = 1, DivisionId = 1, Name = "IT" },
new Models.Division { CompanyId = 1, DivisionId = 2, Name = "Purchasing" },
new Models.Division { CompanyId = 1, DivisionId = 3, Name = "Finance" },
new Models.Division { CompanyId = 1, DivisionId = 4, Name = "Production" },
new Models.Division { CompanyId = 1, DivisionId = 5, Name = "Retail" },
new Models.Division { CompanyId = 2, DivisionId = 6, Name = "HR" },
new Models.Division { CompanyId = 2, DivisionId = 7, Name = "Cafe" },
new Models.Division { CompanyId = 2, DivisionId = 8, Name = "Projects" }
);
context.SubDivisions.AddOrUpdate(p => p.Name,
new Models.SubDivision { CompanyId = 1, DivisionId = 1, SubDivisionId = 1, Name = "Application" },
new Models.SubDivision { CompanyId = 1, DivisionId = 1, SubDivisionId = 2, Name = " Infra" },
new Models.SubDivision { CompanyId = 1, DivisionId = 1, SubDivisionId = 3, Name = "MIS" },
new Models.SubDivision { CompanyId = 1, DivisionId = 2, SubDivisionId = 1, Name = "Purchasing" },
new Models.SubDivision { CompanyId = 1, DivisionId = 3, SubDivisionId = 1, Name = "Finance" },
new Models.SubDivision { CompanyId = 1, DivisionId = 4, SubDivisionId = 1, Name = "Production" },
new Models.SubDivision { CompanyId = 1, DivisionId = 5, SubDivisionId = 1, Name = "Retail" },
new Models.SubDivision { CompanyId = 2, DivisionId = 5, SubDivisionId = 1, Name = "Admin" },
new Models.SubDivision { CompanyId = 2, DivisionId = 6, SubDivisionId = 1, Name = "Cafe" },
new Models.SubDivision { CompanyId = 2, DivisionId = 7, SubDivisionId = 1, Name = "Projects" }
);
}
}
and ApplicationDbContext file as below
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Entity<ApplicationUser>().Property(a => a.Latitude).HasPrecision(18, 9);
modelBuilder.Entity<ApplicationUser>().Property(a => a.Longitude).HasPrecision(18, 9);
base.OnModelCreating(modelBuilder);
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<Company> Companies { get; set; }
public DbSet<Division> Divisions { get; set; }
public DbSet<SubDivision> SubDivisions { get; set; }
public System.Data.Entity.DbSet<webapp_sample.Models.MainMenu> MainMenus { get; set; }
}
Do you use the EF7?
You can create a class:
public class DataInitializer
{
webapp_sample.Models.ApplicationDbContext _context
public void InitializeDataAsync(IServiceProvider serviceProvider)
{
_context = serviceProvider.GetService<webapp_sample.Models.ApplicationDbContext>();
// Add the code for inintializing at here
...
}
}
Also you need to use _context.SaveChanges();.
In the Startup.cs file, you can do somethings as follow:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
DataInitializer dataseed = new DataInitializer();
dataseed.InitializeDataAsync(app.ApplicationServices);
}
Hope this help!
I encountered a problem with popup control inside modal edit form of the gridview.
the problem is: popup control won't hide when i click on modal form context area! it just reordered.
please see attached movie first then read the code.
thanks.
here is my code:
MODEL:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Family { get; set; }
public int Avg { get; set; }
}
CONTROLLER:
public class HomeController : Controller
{
public ActionResult Index()
{
Session["Students"] = new List<Student>()
{
new Student { Id = 1, Name = "N1", Family = "f1", Avg = 1 } ,
new Student { Id = 2, Name = "N2", Family = "f2", Avg = 2 } ,
new Student { Id = 3, Name = "N3", Family = "f3", Avg = 3 } ,
new Student { Id = 4, Name = "N4", Family = "f4", Avg = 4 } ,
};
return View(Session["Students"]);
}
public ActionResult PartialGridView(Student student)
{
return PartialView("Index", Session["Students"]);
}
}
View
#model List<Test.Models.Student>
#Html.DevExpress().GridView(settings =>
{
settings.Name = "GvStudent";
settings.KeyFieldName = "Id";
settings.CallbackRouteValues = new { Controller = "Home", Action = "PartialGridView" };
settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Home", Action = "AddStudentPartialGridView" };
settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Home", Action = "UpdateStudentPartialGridView" };
settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Home", Action = "DeleteStudentPartialGridView" };
settings.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
settings.SettingsPopup.EditForm.Modal = true;
settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.WindowCenter;
settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
settings.SettingsPopup.EditForm.Width = 400;
settings.SettingsPopup.EditForm.Height = 200;
settings.SettingsText.PopupEditFormCaption = "Student";
settings.CommandColumn.Visible = true;
settings.CommandColumn.NewButton.Visible = true;
settings.CommandColumn.EditButton.Visible = true;
settings.CommandColumn.DeleteButton.Visible = true;
settings.Columns.Add(Name =>
{
Name.FieldName = "Name";
});
settings.Columns.Add(Family =>
{
Family.FieldName = "Family";
});
settings.Columns.Add(Avg =>
{
Avg.FieldName = "Avg";
});
settings.SetEditFormTemplateContent(content =>
{
Html.DevExpress().Button(btnShow =>
{
btnShow.Name="btnShow";
btnShow.ClientSideEvents.Click = "function(s, e){popupControl.ShowAtElement(document.getElementById(s.name));}";
}).Render();
});
}).Bind(Model).GetHtml()
#{
#Html.DevExpress().PopupControl(settings =>
{
settings.Name = "popupControl";
settings.CloseAction = CloseAction.OuterMouseClick;
settings.PopupVerticalAlign = PopupVerticalAlign.Below;
settings.PopupHorizontalAlign = PopupHorizontalAlign.LeftSides;
settings.ShowHeader = false;
settings.ShowFooter = false;
settings.AllowDragging = false;
settings.AutoUpdatePosition = true;
settings.Width = 320;
settings.SetContent(() =>
{
ViewContext.Writer.Write("hello world!");
});
}).GetHtml();
}
Unit testing of the role provider to fail.
[TestMethod]
public void FindUsersInRole()
{
Mock<IUsersInRoleRepository> userInRoleMock = new Mock<IUsersInRoleRepository>();
userInRoleMock.Setup(m => m.UsersInRoles).Returns(new UsersInRole[] {
new UsersInRole { UserId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), RoleId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") },
new UsersInRole { UserId = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"), RoleId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") },
new UsersInRole { UserId = Guid.Parse("cccccccc-cccc-cccc-cccc-cccccccccccc"), RoleId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") },
new UsersInRole { UserId = Guid.Parse("dddddddd-dddd-dddd-dddd-dddddddddddd"), RoleId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") },
new UsersInRole { UserId = Guid.Parse("eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee"), RoleId = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb") }
}.AsQueryable());
Mock<IRoleRepository> roleMock = new Mock<IRoleRepository>();
roleMock.Setup(m => m.Roles).Returns(new Role[] {
new Role { RoleId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), RoleName = "test" },
new Role { RoleId = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"), RoleName = "admin" }
}.AsQueryable());
Mock<IUserRepository> userMock = new Mock<IUserRepository>();
userMock.Setup(m => m.Users).Returns(new User[] {
new User { UserId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), UserAccount = "abcdef" },
new User { UserId = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"), UserAccount = "bcdef" },
new User { UserId = Guid.Parse("cccccccc-cccc-cccc-cccc-cccccccccccc"), UserAccount = "cdef" },
new User { UserId = Guid.Parse("dddddddd-dddd-dddd-dddd-dddddddddddd"), UserAccount = "bcdf" },
new User { UserId = Guid.Parse("eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee"), UserAccount = "abcde" }
}.AsQueryable());
RoleProvider target = new RoleProvider(userMock.Object, roleMock.Object, userInRoleMock.Object);
string[] result = target.FindUsersInRole("test", "cde");
Assert.AreEqual(result[0], "abcdef");
Assert.AreEqual(result[1], "bcdef");
Assert.AreEqual(result[2], "cdef");
}
Unit Test Code
string[] result = target.FindUsersInRole("test", "cde"); <-- error
FindUsersInRole - Gets an array of user names in a role where the user name contains the specified user name to match.
System.NullReferenceException is raised and try to debug.
Why NullReferenceException?
PS - FindUsersInRole (RoleProvider)
public override string[] FindUsersInRole(string roleName, string userAccountToMatch)
{
Guid roleId = roleRepository.GetRole(roleName).RoleId; // RoleId Retrun.. NullReferenceException
var roleInUsers = (from ru in usersInRoleRepository.UsersInRoles
where ru.RoleId == roleId
select ru.UserId).ToArray();
var findUserResult = (from u in userRepository.Users
where roleInUsers.Contains(u.UserId) && u.UserAccount.Contains(userAccountToMatch)
select u.UserAccount).ToArray();
return findUserResult;
}
Your cde is not a fake user in a mackUserAccount . So its return null.
try this below code string[] result = target.FindUsersInRole("test", "abcdef"); instead of
string[] result = target.FindUsersInRole("test", "cde");
I'm new to Entity Framework, which I'm using for an ASP.NET MVC4 project. I'm trying to create a foreign key relationship and am not having much success. The problem may be in my seed data, which I'm not sure how to restructure from an earlier attempt to get this working.
Here are my two classes:
public class HardwareType
{
public int Id { get; set; }
[Required]
[StringLength(128)]
public string HType { get; set; }
public int HardwareId { get; set; }
public virtual Hardware Hardware { get; set; }
}
and
public class Hardware
{
public int Id { get; set; }
//public int HardwareId { get; set; }
...
public virtual ICollection<HardwareType> HardwareType { get; set; }
}
Here is my sample seed data:
protected override void Seed(Context context)
{
var loc = new List<Location> {
new Location { LocationName = "Paradise Lane" },
new Location { LocationName = "81st Street" }
};
loc.ForEach(l => context.Locations.Add(l));
var type = new List<SoftwareType> {
new SoftwareType { SType = "Suite" }
};
type.ForEach(t => context.SoftwareTypes.Add(t));
var pub = new List<SoftwarePublisher> {
new SoftwarePublisher { Publisher = "Adobe" },
new SoftwarePublisher { Publisher = "Apple" },
new SoftwarePublisher { Publisher = "Microsoft" }
};
var soft = new List<Software> {
new Software { Title = "Adobe Creative Suite", Version = "CS6", SerialNumber = "1234634543", Platform = "Mac", Notes = "Macs rock!", PurchaseDate = "2012-12-04", Suite = true, SubscriptionEndDate = null, SeatCount = 0, Locations = loc.Where(s => s.LocationName == "Paradise Lane").ToArray(), Types = type.Where(s => s.SType == "Suite").ToArray(), Publishers = pub.Where(s => s.Publisher == "Adobe").ToArray() },
new Software { Title = "Microsoft Office", Version = "2012", SerialNumber = "34252345", Platform = "PC", Notes = "PCs stink!", PurchaseDate = "2012-11-04", Suite = true, SubscriptionEndDate = null, SeatCount = 0, Locations = loc.Where(s => s.LocationName == "81st Street").ToArray(), Types = type.Where(s => s.SType == "Suite").ToArray(), Publishers = pub.Where(s => s.Publisher == "Microsoft").ToArray() },
new Software { Title = "Apple iLife", Version = "2012", SerialNumber = "54675747564", Platform = "Mac", Notes = "Macs still rock!", PurchaseDate = "2012-12-04", Suite = true, SubscriptionEndDate = null, SeatCount = 0, Locations = loc.Where(s => s.LocationName == "Paradise Lane").ToArray(), Types = type.Where(s => s.SType == "Suite").ToArray(), Publishers = pub.Where(s => s.Publisher == "Apple").ToArray() }
};
soft.ForEach(s => context.Software.Add(s));
var manuf = new List<Manufacturer> {
new Manufacturer { ManufacturerName = "SonicWall" },
new Manufacturer { ManufacturerName = "Cisco" },
new Manufacturer { ManufacturerName = "Barracuda" },
new Manufacturer { ManufacturerName = "Dell" },
new Manufacturer { ManufacturerName = "HP" },
new Manufacturer { ManufacturerName = "Maxtor" },
new Manufacturer { ManufacturerName = "LaCie" },
new Manufacturer { ManufacturerName = "APC" },
new Manufacturer { ManufacturerName = "Intel" },
new Manufacturer { ManufacturerName = "D-Link" },
new Manufacturer { ManufacturerName = "Western Digital" },
new Manufacturer { ManufacturerName = "Quantum" },
new Manufacturer { ManufacturerName = "Seagate" },
new Manufacturer { ManufacturerName = "Apple" },
new Manufacturer { ManufacturerName = "Canon" },
};
var device = new List<DeviceType> {
new DeviceType { DType = "Network Device"},
new DeviceType { DType = "Other"}
};
var htype = new List<HardwareType> {
new HardwareType { HType = "PC" },
new HardwareType { HType = "Monitor" },
new HardwareType { HType = "Printer" },
new HardwareType { HType = "Miscellaneous" }
};
var hard = new List<Hardware> {
new Hardware { AssetTagId = "2134", Type = device.Where(h => h.DType == "Network Device").ToArray(), Manufacturer = manuf.Where(h => h.ManufacturerName == "SonicWall").ToArray(), ServiceTagId = "5243", SerialNumber = "3456", ProductNumber = "2345", PurchaseDate = "2012-10-23", WarrantyExpiration = "2012-11-12", WarrantyType = "NBD", Location = loc.Where(h => h.LocationName == "Paradise Lane").ToArray(), Notes = "Scrapped", HardwareType = htype.Where(h => h.HType == "Monitor").ToArray()},
new Hardware { AssetTagId = "2134", Type = device.Where(h => h.DType == "Network Device").ToArray(), Manufacturer = manuf.Where(h => h.ManufacturerName == "SonicWall").ToArray(), ServiceTagId = "5243", SerialNumber = "3456", ProductNumber = "2345", PurchaseDate = "2012-10-23", WarrantyExpiration = "2012-11-12", WarrantyType = "NBD", Location = loc.Where(h => h.LocationName == "Paradise Lane").ToArray(), Notes = "Scrapped", HardwareType = htype.Where(h => h.HType == "PC").ToArray() },
new Hardware { AssetTagId = "2134", Type = device.Where(h => h.DType == "Network Device").ToArray(), Manufacturer = manuf.Where(h => h.ManufacturerName == "SonicWall").ToArray(), ServiceTagId = "5243", SerialNumber = "3456", ProductNumber = "2345", PurchaseDate = "2012-10-23", WarrantyExpiration = "2012-11-12", WarrantyType = "NBD", Location = loc.Where(h => h.LocationName == "Paradise Lane").ToArray(), Notes = "Scrapped", HardwareType = htype.Where(h => h.HType == "PC").ToArray() }
};
hard.ForEach(h => context.Hardwares.Add(h));
base.Seed(context);
}
How do I structure this so the foreign key relationship works. Right now, I get this error: Unable to set field/property HardwareType on entity type CIT.Models.Hardware. See InnerException for details.
The inner exception is: An item cannot be removed from a fixed size Array of type 'CIT.Models.HardwareType[]'.
try changing you'r tables structure like this :
public class Hardware
{
public int Id { get; set; }
.
.
.
public HardwareTypeId { get; set; }
public virtual HardwareType hardwareType {get;set;}
}
and HardwareType :
public class HardwareType
{
public int Id { get; set; }
public string TypeName {get;set;}
}
I am trying to construct a navigation menu using a Categories table from my db.
I have a similar layout as below in Categories table.
public List<Category> CategoryData = new List(new Category[] {
new Category{ CategoryId = 1, Name = "Fruit", ParentCategoryId = null},
new Category{ CategoryId = 2, Name = "Vegetables", ParentCategoryId = null},
new Category{ CategoryId = 3, Name = "Apples", ParentCategoryId = 1},
new Category{ CategoryId = 4, Name = "Bananas", ParentCategoryId = 1},
new Category{ CategoryId = 5, Name = "Cucumber", ParentCategoryId = 2},
new Category{ CategoryId = 6, Name = "Onions", ParentCategoryId = 2}
); }
The above should return something like
Fruit (parent)
"===Apples, Bananas (child)
Vegetables (parent)
"===Cucumber, Onions (child)
I need to be able to pass this as some kind of 'grouped' (grouped by parentid) collection to my View.
How to do this?
It would seem like this is a good example of when translating your model to a viewModel would come in handy. As you could create a collection of CategoryViewModel which have a Childrens property of CategoryViewModel using the same technique describe by #thomas.
public class CategoryViewModel
{
public int CategoryId { set; get; }
public string CategoryName { set; get; }
public int? ParentCategoryId { set; get; }
public IEnumerable<CategoryViewModel> Children { set; set; }
}
public static IEnumerable<CategoryViewModel> GetAllCategoryViewModel(IList<Category> categories)
{
var query = GetChildren(null, categories);
return query.ToList();
}
public static IEnumerable<CategoryViewModel> GetChildren(int? parentId, IList<Category> categories)
{
var children = from category in categories
where category.ParentCategoryId == parentId
select
new CategoryViewModel
{
CategoryId = category.CategoryId,
CategoryName = category.CategoryName,
ParentCategoryId = category.ParentCategoryId,
Children = GetChildren(category.CategoryId, categories)
};
return children;
}
How about something like:
private void Test()
{
var categoryData = new List
{
new Category {CategoryId = 1, Name = "Fruit", ParentCategoryId = null},
new Category {CategoryId = 2, Name = "Vegetables", ParentCategoryId = null},
new Category {CategoryId = 3, Name = "Apples", ParentCategoryId = 1},
new Category {CategoryId = 4, Name = "Bananas", ParentCategoryId = 1},
new Category {CategoryId = 5, Name = "Cucumber", ParentCategoryId = 2},
new Category {CategoryId = 6, Name = "Onions", ParentCategoryId = 2}
};
var query = from category in categoryData
where category.ParentCategoryId == null
select category;
foreach ( var item in query )
{
Debug.WriteLine( string.Format( "{0} (parent)", item.Name ) );
Debug.WriteLine( GetChildren(item.CategoryId, categoryData ) );
}
}
private static string GetChildren( int parentCategoryId, IEnumerable categoryData)
{
var children = ( from category in categoryData
where category.ParentCategoryId == parentCategoryId
select category.Name ).ToArray();
return string.Format( "==={0} (child)", string.Join( ", ", children ) );
}
var list = from a in CategoryData
join b in CategoryData on a.ParentCategoryId equals b.CategoryId into c
from d in c.DefaultIfEmpty()
where d != null
select new {
a.CategoryId,
a.Name,
a.ParentCategoryId,
ParentName = d.Name
};
returns
CategoryId Name ParentCategoryId ParentName
3 Apples 1 Fruit
4 Bananas 1 Fruit
5 Cucumber 2 Vegetables
6 Onions 2 Vegetables
You can then loop through it in your view and format accordingly.