I want to put the list of deleted comments in the admin section. IsDelete capability also has a comments section. The code works fine, but when I put the where condition, the output value is zero and no comments are displayed.
public ShowCommentUsersForAdminViewModel GetRemovedCommentByAdminSearch(int pageId = 1, string filter = "")
{
//lazy load enable with IQueryable
IQueryable<BlogComment> result = _context.BlogComments
.Include(c => c.Blog)
.Include(c => c.User);
if (!string.IsNullOrEmpty(filter))
{
result = result.Where(u => u.Comment.Contains(filter) || u.User.UserName.Contains(filter) || u.Blog.BlogTitle.Contains(filter));
}
//show Item In Page
int take = 10;
int skip = (pageId - 1) * take;
ShowCommentUsersForAdminViewModel list = new ShowCommentUsersForAdminViewModel();
list.CurrentPage = pageId;
list.PageCount = (result.Count() / take) + 1;
list.BlogComments = result.Where(c => c.IsDelete).OrderBy(u => u.CreateDateComment).Skip(skip).Take(take).ToList();
return list;
}
yes I used this code
modelBuilder.Entity<ProductComment>().HasQueryFilter(b => !b.IsDelete);
Reviewed the code again, and used IgnoreQueryFilters() problem solved.
thanks Ivan Stoev
Related
I am working on a project which is an E-health patient Gateway.
I want to generate a number (int type) against a submit form from View to Controller (When phlebotomist receives the blood sample he will check the box and click on submit button). This number is the patient's blood sample number that will be generated in my controller and will be saved in the database. I am using the logic below to create the number but it always gives me zero.
Kindly let me know how I can create numbers one by one in sequence and store in database?
public ActionResult AddSampleTest(int Pid, int Tid, int Stid, string Comment ,string testDate,int DotorID)
{
int sampleNumber=1;
Random rnd = new Random();
int[] sample = new int[50000];
rnd.Next();
for (int ctr = 1; ctr <= sample.Length; ctr++)
{
sampleNumber=sample[ctr + 1];
}
string sampleno = sampleNumber.ToString();
DateTime TestDate = Convert.ToDateTime(testDate);
int Recomended_Test_DoctorID = DotorID;
// Update the Sample Status
PatientTest objpatientTest = db.PatientTests.Where(x => x.PatientID == Pid && x.Testid == Tid && x.SubTestId == Stid && x.Date==TestDate &&x.DoctorId== Recomended_Test_DoctorID).FirstOrDefault();
objpatientTest.SampleStatus = "Sample Received";
objpatientTest.SampleNumber = sampleno;
db.SaveChanges();
var name = User.Identity.Name;
int Lid = db.LabAttendantRecords.Where(x => x.Name == name).Select(x => x.User_id).FirstOrDefault();
LabTestSample obj = new LabTestSample();
obj.labtestid = Tid;
obj.PatientId = Pid;
obj.subtestid = Stid;
obj.labAtendid = Lid;
obj.date = DateTime.Now.Date;
obj.sampleReceived = true;
obj.Comment = sampleno;
db.LabTestSamples.Add(obj);
db.SaveChanges();
return RedirectToAction("TestSampleNumber", "LabAttendantDashboard", new { sampleno});
// Use This for add Result
return RedirectToAction("SubTest", "LabAttendantDashboard", new { Pid, Tid, Stid });
}
sampleNumber is always 0 because each element in sample is 0.
If you want it to be a random number you need to do something like :
Random rnd = new Random();
int sampleNumber = rnd.Next();
string sampleno = sampleNumber.ToString();
If you need to be sure that the sample number is unique, you should use a Guid :
string sampleno = Guid.NewGuid().ToString();
I have some code that is selecting some values from my database. I have a IQueryable of requests, that contains a ICollection of rooms.
I want to get a List<ICollection<rooms>>. I have tried the following and i recieve the above error.
Any ideas??
public ActionResult _roomChecker(checkRooms JSONdata){
var rooms = db.rooms.Include(r=>r.building).Include(r=>r.facilities);
rooms = rooms.Where(r => r.capacity >= JSONdata.capacity);
if (JSONdata.type != "Any")
{
rooms = rooms.Where(r => r.roomType.Equals(JSONdata.type));
}
if (JSONdata.park != "Any")
{
rooms = rooms.Where(r => r.building.park.Equals(JSONdata.park));
}
if (JSONdata.facilities != null)
{
for (var i = 0; i < JSONdata.facilities.Length; i++)
{
rooms = rooms.Where(r => r.facilities.Any(f => f.facilityName.Equals(JSONdata.facilities[i])));
}
}
var proposedRequest = db.requests.Include(r => r.rooms);
proposedRequest = proposedRequest.Where(r=>r.booked.Equals(1));
proposedRequest = proposedRequest.Where(r => r.roundID.Equals(JSONdata.roundID));
proposedRequest = proposedRequest.Where(r => r.day.Equals(JSONdata.day));
proposedRequest = proposedRequest.Where(s => s.start < JSONdata.start + JSONdata.length && s.start + s.length > JSONdata.start);
int[] standardWeeks = new int[12] {1,2,3,4,5,6,7,8,9,10,11,12};
var containsStandard = standardWeeks.Intersect(JSONdata.weeks);
if (containsStandard.Count()!=0)
{
proposedRequest = proposedRequest.Where(r => r.weeks_request.Any(f => JSONdata.weeks.Contains(f.week)) || r.weeks.Equals(1));
}
else {
proposedRequest = proposedRequest.Where(r => r.weeks_request.Any(f => JSONdata.weeks.Contains(f.week)));
}
//ERROR OCCURS ON THIS LINE BELOW
List<ICollection<room>> bookedRooms = proposedRequest.Select(r => r.rooms).ToList();
var deptRooms = db.rooms.Include(r => r.building).Include(r => r.facilities).Where(r => r.belongsTo.Equals(JSONdata.deptCode));
roomCheckerObject suitableRooms = new roomCheckerObject();
suitableRooms.code = JSONdata.deptCode;
suitableRooms.roomNo = JSONdata.roomNo;
suitableRooms.RequestNo = JSONdata.RequestNo;
if(rooms.Count() >0){
suitableRooms.rooms = rooms.ToList();
var buildings = rooms.Select(r => r.building).Distinct();
suitableRooms.buildings = buildings.ToList();
}
if(bookedRooms.Count() >0){
suitableRooms.bookedRooms = bookedRooms;
}
if(deptRooms.Count() >0){
suitableRooms.deptRooms = deptRooms.ToList();
}
return PartialView(suitableRooms);}
It's kind of difficult to say what exactly is causing the error but I can see 3 possible culprits:
1) You're using .Equals() with a Linq to entities (L2E) query. This can cause problems, see here.
2) You say the error occurs on the line where List<ICollection<room>> is declared. On this line you call .ToList() for the first time in any of L2E statements. Only now will your L2E query be executed (see EF Query Execution) against the database meaning possibly any error (using .Equals()) in a previous L2E statement could throw the exception.
3) Does proposedRequest.Select(r => r.rooms).ToList(); really return an List<ICollection<room>>? Try using .SelectMany() (though this may have undesirable effects) or for a sanity check change List<ICollection<room>> to var to see what the query will return.
I’m trying to update a row in a table. I found a way to make it work but I’m wondering if there is an easier or better way. Please help me with any ideas that you may have. Thanks
Here’s the code…
var courseRubics = from r in db.Rubrics where r.DepartmentID == 2 select r;
var selectedRubrics = courseRubics.Select(r => r.RubricID);
List<int> rubricsList = selectedRubrics.ToList();
foreach (var rub in courseRubics.ToList())
{
if (!String.IsNullOrEmpty(formCollection["item.Weight"]))
{
Rubric aRubic1 = db.Rubrics.Find(1);
Rubric updateRubic1 = (Rubric)aRubic1;
int rubric1 = Convert.ToInt32(totlrubric[0]);
updateRubic1.Weight = rubric1;
Rubric aRubic2 = db.Rubrics.Find(2);
Rubric updateRubic2 = (Rubric)aRubic2;
int rubric2 = Convert.ToInt32(totlrubric[1]);
updateRubic2.Weight = rubric2;
Rubric aRubic3 = db.Rubrics.Find(4);
Rubric updateRubic3 = (Rubric)aRubic3;
int rubric3 = Convert.ToInt32(totlrubric[2]);
updateRubic3.Weight = rubric3;
}
db.SaveChanges();
}
Couple of ideas / observations
Your initial set of selects could be narrowed down to (sorry for the alternate syntax)
var list = db.Rubrics.Where(r => r.DepartmentId == 2).Select(x => x.RubricId).ToList()
You iterate over foreach (var rub in courseRubics.ToList()) but don't seem to be doing anything with rub
The contents of the if... could be tightened up a little. I don't see any reason why you need to do the casting or why you can't just inline the creation of rubricX,
if (!String.IsNullOrEmpty(formCollection["item.Weight"]))
{
Rubric aRubic1 = db.Rubrics.Find(1);
aRubic1.Weight = Convert.ToInt32(totlrubric[0]);
Rubric aRubic2 = db.Rubrics.Find(2);
aRubic2.Weight = Convert.ToInt32(totlrubric[1]);
Rubric aRubic3 = db.Rubrics.Find(4);
aRubic3.Weight = Convert.ToInt32(totlrubric[2]);
}
I have some problems with LINQ and maby someone got answers
string[] roleNames = Roles.GetRolesForUser(currentUserName);
result = context.MenuRoles.Select(mr => new MenuGenerateViewModel
{
MenuID = mr.MenuID,
MenuNazwa = mr.Menu.MenuNazwa,
MenuKolejnosc = mr.Menu.MenuKolejnosc,
MenuStyl = mr.Menu.MenuStyl,
MenuParentID = mr.Menu.MenuParentID,
MenuActive = mr.Menu.MenuActive,
MenuActionName = mr.Menu.MenuAction.MenuActionName,
MenuControlName = mr.Menu.MenuControl.MenuControlName,
RoleName = mr.Role.RoleName,
RoleID = mr.RoleID,
MenuID = mr.MenuID
})
.Where(mr => mr.MenuActive == true)
.ToList();
How to take only compare string[] roleNames and return only if match. Problem alwais is when user is in the 2 or more roles.
Tx for answers
If I understand what you are asking for, add a second condition to your Where clause:
.Where(mr => mr.MenuActive && roleNames.Contains(mr.Role.RoleName))
You would be better off switching round your Where clause and Select for the simple reason that then you will not be retrieving from the database records which are not required.
result = context.MenuRoles.Where(mr => mr.MenuActive
&& roleNames.Contains(mr.Role.RoleName))
.Select(mr => ... )
.ToList();
This will generate a sql which only selects the necessary records, instead of selecting the whole lot and then filtering it. Try it and watch SQL profiler to see the difference (useful skill in any case when using EF)
With the help of brilliant people here, reached the target.
string[] roleNames = Roles.GetRolesForUser(currentUserName);
result = context.MenuRoles
.Where(mr => mr.Menu.MenuActive && roleNames.Contains(mr.Role.RoleName))
.Select(mr => new MenuGenerateViewModel
{
MenuID = mr.MenuID,
MenuNazwa = mr.Menu.MenuNazwa,
MenuKolejnosc = mr.Menu.MenuKolejnosc,
MenuStyl = mr.Menu.MenuStyl,
MenuParentID = mr.Menu.MenuParentID,
MenuActive = mr.Menu.MenuActive,
MenuActionName = mr.Menu.MenuAction.MenuActionName,
MenuControlName = mr.Menu.MenuControl.MenuControlName,
RoleName = mr.Role.RoleName
})
.ToList();
var userresult = context.MenuUsers
.Where(mr => mr.Menu.MenuActive && mr.User.Username == currentUserName)
.Select(mr => new MenuGenerateViewModel
{
MenuID = mr.MenuID,
MenuNazwa = mr.Menu.MenuNazwa,
MenuKolejnosc = mr.Menu.MenuKolejnosc,
MenuStyl = mr.Menu.MenuStyl,
MenuParentID = mr.Menu.MenuParentID,
MenuActive = mr.Menu.MenuActive,
MenuActionName = mr.Menu.MenuAction.MenuActionName,
MenuControlName = mr.Menu.MenuControl.MenuControlName,
Username = mr.User.Username
})
.ToList();
Here, gets all the menu to which you have the right, both through group membership and assigned directly to the menu itself.
// Kick all duplicates
var noduplicates = result.Concat(userresult)
.Distinct(new RoleMenuGenerateComparer());
Because usually we do not want duplicates in the menu so we remove them. For this to work properly we need to implement IEqualityComparer (U can read about this little bit up)
public class RoleMenuGenerateComparer : IEqualityComparer<MenuGenerateViewModel>
{
public bool Equals(MenuGenerateViewModel x, MenuGenerateViewModel y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
//Check whether the products' properties are equal.
return x.MenuNazwa == y.MenuNazwa && x.MenuID == y.MenuID;
}
public int GetHashCode(MenuGenerateViewModel menuGenerateViewModel)
{
if (Object.ReferenceEquals(menuGenerateViewModel, null)) return 0;
int hashMenuName = menuGenerateViewModel.MenuNazwa == null ? 0 : menuGenerateViewModel.MenuNazwa.GetHashCode();
int hashMenuID = menuGenerateViewModel.MenuID == null ? 0 : menuGenerateViewModel.MenuID.GetHashCode();
return hashMenuName ^ hashMenuID;
}
}
Of course I believe that you can optimize this code, but for the moment I have something like this.
Tx all for help.
I have a database driven menu Helper that gets called from within my master page:
<div class="topBar">
<%= Html.MenuTree(39, false, "first", "last") %>
<div class="clear"></div>
</div>
Below is the code that outputs my HTML unordered list. The problem is that sometimes the output of the menu is completely wrong and all over the place ie. sub menu items appear as equal as top menu items.
I cannot find any pattern to why it does it so thought I'd post the code to see if anyone can spot the problem. My only other thought is that somehow its half cached half called and mixes the output.
This is what it should look like Correct http://img718.imageshack.us/img718/9317/screenshot20100328at120.png
Sometimes it comes out like this:alt text http://img413.imageshack.us/img413/9317/screenshot20100328at120.png
Here's the code (the boolean IsAdmin is false in this scenario):
public static string MenuTree(this HtmlHelper helper, int MenuCategoryID, bool Admin, string firstCssClass, string lastCssClass)
{
//TODO: Check for Subsonic fix for UNION bug
IOrderedQueryable<Menu> menuItems;
if (Admin)
{
menuItems = (from menu2 in Menu.All()
join pages in WebPage.All() on menu2.PageID equals pages.ID
join pagesRoles in PageRole.All() on pages.ID equals pagesRoles.PageID
join roles in aspnet_Role.All() on pagesRoles.RoleId equals roles.RoleId
where Roles.GetRolesForUser().Contains(roles.RoleName) && menu2.CategoryID == MenuCategoryID && menu2.Visible
select menu2).Distinct().OrderBy(f => f.OrderID);
}
else
{
menuItems = (from menu2 in Menu.All()
join pages in WebPage.All() on menu2.PageID equals pages.ID
where menu2.CategoryID == MenuCategoryID && menu2.Visible
select menu2).Distinct().OrderBy(f => f.OrderID);
}
var nonlinkedmenuItems = (from menu in Menu.All().Where(x => x.PageID == null && x.CategoryID == MenuCategoryID && x.Visible).OrderBy(f => f.OrderID) select menu);
var allCategories = menuItems.ToList().Concat<Menu>(nonlinkedmenuItems.ToList()).OrderBy(p => p.OrderID).ToList();
allCategories.ForEach(x => x.Children = allCategories.Where(y => y.ParentID == x.ID).OrderBy(f => f.OrderID));
Menu home = null;
if (Admin)
{
home = (from menu in Menu.All()
join pages in WebPage.All() on menu.PageID equals pages.ID
where pages.MenuName == "Home" && pages.IsAdmin
select menu).SingleOrDefault();
}
IEnumerable<Menu> topLevelItems;
if (Admin)
topLevelItems = allCategories.Where(f => f.ParentID == 0 && (f.Children.Count() > 0 || f.ID == home.ID));
else
topLevelItems = allCategories.Where(f => f.ParentID == 0);
var topLevelItemList = topLevelItems.ToList();
sbMenu.Length = 0;
sbMenu.AppendLine("<ul>");
LoopChildren(helper, Admin, topLevelItemList, 0, firstCssClass, lastCssClass);
sbMenu.AppendLine("</ul>");
string menuString = sbMenu.ToString();
//if ((menuString.IndexOf("<li>")) > 0)
// menuString = menuString.Insert((menuString.IndexOf("<li>") + 3), " class='first'");
//if (menuString.LastIndexOf("<li>\r\n") > 0)
// menuString = menuString.Insert((menuString.LastIndexOf("<li>\r\n") + 3), " class='last'");
return sbMenu.ToString();
}
private static void LoopChildren(this HtmlHelper helper, bool Admin, List<Menu> CurrentNode, int TabIndents, string firstCssClass, string lastCssClass)
{
for (int i = 0; i < CurrentNode.Count; i++)
{
sbMenu.Append(Tabs(TabIndents + 1));
string linkUrl = "";
string urlTitle = "";
if (CurrentNode[i].PageID != null)
{
WebPage item = WebPage.SingleOrDefault(x => x.ID == CurrentNode[i].PageID);
linkUrl = item.URL;
urlTitle = item.MenuName;
}
else
{
linkUrl = CurrentNode[i].URL;
urlTitle = CurrentNode[i].Title;
}
//Specify a RouteLink so that when in Error 404 page for example the links don't become /error/homepage
//If in admin we can manually write the <a> tag as it has the controller and action in it
bool selected = false;
if (helper.ViewContext.RouteData.Values["pageName"] != null && helper.ViewContext.RouteData.Values["pageName"].ToString() == linkUrl)
selected = true;
string anchorTag = Admin ? "<a href='" + linkUrl + "'>" + urlTitle + "</a>" : helper.RouteLink(urlTitle, new { controller = "WebPage", action = "Details", pageName = linkUrl });
if (TabIndents == 0 && i == 0 && firstCssClass != null)
sbMenu.AppendLine("<li class='" + firstCssClass + "'>" + anchorTag);
else if (TabIndents == 0 && i == (CurrentNode.Count - 1) && lastCssClass != null)
sbMenu.AppendLine("<li class='" + lastCssClass + "'>" + anchorTag);
else if (selected)
sbMenu.AppendLine("<li class='selected'>" + anchorTag);
else
sbMenu.AppendLine("<li>" + anchorTag);
if (CurrentNode[i].Children != null && CurrentNode[i].Children.Count() > 0)
{
sbMenu.Append(Tabs(TabIndents + 2));
sbMenu.AppendLine("<ul>");
LoopChildren(helper, Admin, CurrentNode[i].Children.ToList(), TabIndents + 2, "", "");
sbMenu.Append(Tabs(TabIndents + 2));
sbMenu.AppendLine("</ul>");
}
sbMenu.Append(Tabs(TabIndents + 1));
sbMenu.AppendLine("</li>");
}
}
private static string Tabs(int n)
{
return new String('\t', n);
}
I agree with the comments that string concatenation for this is painful. TagBuilder is a lot less painful for you.
I didn't check your code for problems, but I imagine that what I would do is basically to take the text output from your helper in a good case and a bad case and run them through a diff tool. Leave some markers before and after the point where you call Html.MenuTree() for debugging purposes - this way you will know exactly where the output starts and stops.
The diff tool will tell you what the differences in the two outputs are. Then you can go looking for the cause of these differences.
Another way I would seriously consider approaching this is through unit testing. Start with a simple unit test giving the MenuTree() method a very simple structure to work with. Verify that the output is sane. Then test more complex scenarios. If you during testing, debugging or in production discover a certain combination of input that causes the problem, write a unit test that tests for the correct output. Then fix it. When the test passes, you'll know that you are finished. Also, if you run your tests whenever you change something, you will know that this particular bug will never creep back in.
New bug? New unit test. And so on. Unit tests never solve the problem for you, but they give you the confidence to know that what used to work still works, even when you refactor and come up with cool new stuff.