Skip to content

Instantly share code, notes, and snippets.

View mikecole's full-sized avatar

Mike Cole mikecole

View GitHub Profile
private IEnumerable<string> GetItems()
{
var result = new List<string>();
result.Add("Test1");
result.Add("Test2");
result.Add("Test3");
return result;
}
private IEnumerable<string> GetItems()
{
yield return "Test1";
yield return "Test2";
yield return "Test3";
}
<asp:DropDownList ID="ddlItems" runat="server">
<asp:ListItem Text="<--Select Item-->" Value=""></asp:ListItem>
<asp:ListItem Text="Item 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Item 2" Value="2"></asp:ListItem>
<asp:ListItem Text="Item 3" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="ddlItems" runat="server" ErrorMessage="Please select an item" Text="Please select an item"></asp:RequiredFieldValidator>
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
public class Person
{
private string _firstName;
private string _lastName;
private int _age;
public string FirstName
{
get
{
@mikecole
mikecole / multiple-sources.cs
Created March 7, 2014 15:44
Automapper Multiple Sources
public ActionResult Index(int accountId)
{
var account = _accountContext.Accounts.SingleOrDefault(acct => acct.ID == accountId);
var history = _financialContext.AccountHistory.Where(acct => acct.ID == accountId);
var paymentInfo = _paymentProcessor.GetPaymentProfile(accountId);
var sources = new Tuple<Account, IEnumerable<Transaction>, PaymentProfile>(account, history, paymentInfo);
@mikecole
mikecole / multiple-sources-viewmodel.cs
Created March 7, 2014 16:00
Automapper Multiple Sources
public class AccountSummaryViewModel
{
//Comes from account
public int AccountID { get; set; }
public string AccountName { get; set; }
public DateTime ExpirationDate { get; set; }
//Comes from paymentInfo
public string PaymentMethod { get; set; }
@mikecole
mikecole / gist:2a99dfea0197b724dab8
Last active August 29, 2015 14:16
Enum Display Name Dictionary
class Program
{
static void Main(string[] args)
{
var x = Operations[Operation.NotEquals];
Console.WriteLine(x);
Console.ReadKey();
}
enum Operation
@mikecole
mikecole / lazy-loading-bad
Created September 20, 2013 15:40
Lazy Loading Issue
var posts = context.Posts.ToArray();
var model = (from post in posts
select new PostsViewModel
{
Title = post.Title,
Url = post.Url,
AuthorName = post.Author.Name,
AuthorTwitterHandle = post.Author.TwitterHandle
}).ToArray();
@mikecole
mikecole / eager-loading-good
Created September 20, 2013 16:21
Eager Loading
//Uses string notation - lots of typos!
//var posts = context.Posts
// .Include("Author")
// .ToArray();
//Uses lambda notation - Intellisense and easy refactoring = love
//Note: include System.Data.Entity to use.
var posts = context.Posts
.Include(p => p.Author)
.ToArray();