Skip to content

Instantly share code, notes, and snippets.

View yicone's full-sized avatar

yicone

View GitHub Profile
@yicone
yicone / gist:5105180
Last active December 14, 2015 15:08
从集合里取出来的,还是原来的引用吗
void Main()
{
// 初始化一个context
var cxt = new Context();
cxt.StepModel1 = new StepModel1{ Name = "foo1" };
cxt.StepModel2 = new StepModel2{ Name = "foo2" };
// 从context一个方法的内部的集合中取出一个model
var model1 = cxt.GetStepModel(1);
// 试着更新它
@yicone
yicone / gist:5149927
Created March 13, 2013 07:14
@vwxyzh EF里遇到一个诡异的问题:某表增加了一个字段A(类型为short?),更新了Entity, 程序中第一次使用该Entity的地方,是IQueryable<entity>.Min(r => r.B),当程序连接到另一台未添加字段的数据库,运行时并没有认为表结构有差异,而是返回这么一个异常: Min "The cast to value type 'Int16' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type."
// 修改前报异常。Entity中缺少新增的字段A,但是返回的异常是一个类型转换相关的
short minSecId = queryNoCached.Min(prc => prc.CacheHour);
// 修改后, 运行时容忍了Entity中缺少了新增的字段
short minSecId = queryNoCached.Min(prc => (short?)prc.CacheHour) ?? 0;
@yicone
yicone / gist:5194704
Last active December 15, 2015 03:29
这种情况,GetHaseCode该如何实现?
public class FlightsPlanEqualityComparer : IEqualityComparer<FlightsPlan>
{
public bool Equals(FlightsPlan x, FlightsPlan y)
{
if (x.Flights.Count != y.Flights.Count)
{
return false;
}
for (int i = 0; i < x.Flights.Count; i++)
@yicone
yicone / gist:5381314
Created April 14, 2013 03:30
My first code use MonoDevelop edit, for checking error code used situation in a project.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
namespace CheckUnusedErrorCode
{
class MainClass
@yicone
yicone / gist:5385206
Created April 15, 2013 02:09
load jQuery from Sina CDN
<script type="text/javascript" src="//lib.sinaapp.com/js/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript"> window.jQuery || document.write('<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")">\x3C/script>')</script>
@yicone
yicone / cookie-no-jquery.js
Created April 15, 2013 05:46
cookie operation js, but have a error about cookie path(I'm not sure).
@yicone
yicone / hhSelect.js
Created April 15, 2013 10:36
html下拉框控件。选中待选项时,触发对应的select的change事件。主要参考 [Utom](http://utombox.com/)的Select to CSS,改为用jquery实现。
var HhSelect = {
init: function ($selects) {
$selects.each(function () {
HhSelect._initSelect($(this));
});
// 点击页面任何地方,收起下拉框
$(function () {
$('body').click(function () {
$('div.tag_select_open').attr('class', 'tag_select');
$('ul.tag_options').hide();
@yicone
yicone / GetUrlImpl.cs
Created May 24, 2013 03:15
当初拍脑袋想出来的组合筛选排序+分页的实现。
/// <summary>
/// 生成_ListBar 的各项url
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="baseUrl"></param>
/// <returns></returns>
private string PrepareUrl4ListBar(string name, string value, string baseUrl)
{
string newUrl;
@yicone
yicone / immutable-atomic-value-types.cs
Created May 25, 2013 10:36
http://www.tracefact.net/csharp-programming/immutable-atomic-value-types.aspx 1、当创建类型的目的是为了存储一组相关的数据,且数据量不是很大的时候,将它声明为Struct比Class会获得更高的效率;2、将类型声明为具有原子性和常量性,可以避免可能出现的数据不一致问题;3、通过在构造函数和Get访问器中,对对象的字段进行深度复制,可以避免在类型的外部修改类型内部数据的问题。
void Main()
{
string [] phones = { "18616386880", "18621168732" };
Foo foo = new Foo(phones);
Console.WriteLine(foo.Phones[0]);
phones[0] = "xxx";
Console.WriteLine(foo.Phones[0]);
@yicone
yicone / AllWrapperedProducts.cs
Last active December 17, 2015 19:29
EF优化第一弹
internal override IQueryable<Entity.ProductWrapper> AllWrapperedProducts()
{
DateTime today = DateTime.Now.Date;
var query = (from p in ProductDbContext.Product
where p.ResourceType == (int)ProductResourceType.产品
&& p.PkgId.HasValue && p.PkgId.Value != 0
&& p.ExpireDate.HasValue
&& p.EffectDate.HasValue
&& p.IsValid.HasValue && p.IsValid.Value