This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web.Mvc; | |
namespace MvcApplication21.Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
public ActionResult Index() | |
{ | |
return View(new Models.TestModel { DateNaissance = DateTime.Now }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var context = new MyContext(); | |
var parent = context.Parents.Include("Children").SingleOrDefault(x => x.ParentId == 1); | |
var child = parent.Children.First(); | |
var parent.Childred.Remove(child); // Is this step necessary? | |
var context.Childred.Remove(child); // Or is this alone sufficient? | |
context.SaveChanges(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Parent | |
{ | |
public int ParentId { get; set; } | |
public int Name { get; set; } | |
public string Description { get; set; } | |
public string ExtraInfo { get; set; } | |
public string AnotherExtraInfo { get; set; } | |
public string AndAnotherExtraInfo { get; set; } | |
public virtual ICollection<Child> Children { get; set; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DateTime date = Convert.ToDateTime(xn["date"].InnerText); | |
string dateString; | |
dateString = date.Day + "-"; | |
if (date.Month < 10) | |
{ | |
dateString += "0"; | |
} | |
dateString += date.Month + "-" + date.Year; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var context = new DbContext(); | |
foreach(var itemId in itemIds) // on average about 10 items | |
{ | |
var item = new Item(); | |
item.ForeignKey = itemId; | |
item.SomeProperty = someValue; | |
foreach(var subItemId in subItemsIds) // on average about 20 subitems | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// When I run this code when no previous photo should be found, I get | |
// an exception saying "Query returned multiple rows; cannot return | |
// scalar value.". This is strange because it shouldn't return _any_ | |
// values and because I use ".Take(1)" to make sure only one row is | |
// returned. | |
string previousSlug = DB.Photos | |
.Query() | |
.Select(DB.Photos.Slug) | |
.Where(DB.Photos.Published == true && DB.Photos.DatePublished < photo.DatePublished.Value) // There is no published photo with a smaller DatePublished than the one I check against |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// How one of my ex-colleagues decided to fill a dropdownlist with hour values from "08:00" to "18:00" | |
for (int i = 8; i <= 18; i++) | |
{ | |
ListItem li = new ListItem(); | |
li.Value = i.ToString(); | |
TimeSpan ts = DateTime.Now.AddHours(i) - DateTime.Now; | |
DateTime dtTemp = new DateTime(ts.Ticks); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This also works, but again, it doesn't look very elegant | |
int? previousId; | |
try | |
{ | |
previousId = DB.Photos | |
.FindAll(DB.Photos.Published == true && DB.Photos.DatePublished < currentPhoto.DatePublished.Value) | |
.OrderByDatePublishedDescending() | |
.Take(1) | |
.Select(DB.Photos.Id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code works, but somehow it looks like there is a better and more elegant solution for it. | |
IEnumerable<Models.Photo> previous = DB.Photos | |
.FindAll(DB.Photos.Published == true && DB.Photos.DatePublished < currentPhoto.DatePublished.Value) | |
.OrderByDatePublishedDescending() | |
.Take(1) | |
.Cast<Models.Photo>(); | |
int? previousPhotoId = null; | |
Models.Photo previousPhoto = previous.FirstOrDefault(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<system.webServer> | |
<httpErrors errorMode="Custom"> | |
<remove statusCode="404" /> | |
<error statusCode="404" path="error404.htm" /> | |
</httpErrors> | |
</system.webServer> |
NewerOlder