john's tech blog

hope is coming


  • 首页

  • 标签

  • 归档

entityframework_随想

发表于 2016-08-26 | 更新于 2019-05-09

EntityFramework之DbContext

1. IdentityDbContext 貌似在AutomaticMigrationsEnabled=false时仍然可以生成数据库。DbContext貌似就不可以。

2. AutomaticMigration为true时删除数据库某个表的字段后再新增实体的属性时,有可能会报data loss之类的错误,可能跟_MigrationHistory表有关。

3. 把_Migration表清空后,如果对象新增属性,会报数据库中已存在名为 ‘’ 的对象的错误 。

一开始如果没有用AutomaticMigration生成数据库,之后新增对象属性,如果配置了Database.SetInitializer 那么会报已存在””的对象。

i come back

发表于 2016-08-25 | 更新于 2019-05-07

**将近一年没写博客了,这次回来希望能坚持下去。

csharp-linqmerge

发表于 2015-12-01 | 更新于 2019-05-09

C# Linq合并List中的对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

static void Main(string[] args){

List<Person> pList = new List<Person>();

Person person1 = new Person() { FirstName = "john", LastName = "wonder" };
Person person3 = new Person() { FirstName = "john2", LastName = "wonder22" };
Person person4 = new Person() { FirstName = "john2", LastName = "wonder2233" };

pList.Add(person1);
pList.Add(person3);
pList.Add(person4);


var pMergeList= pList.ToLookup(x => x.FirstName).Select(x => x.Aggregate((p1, p2) =>

new Person{
FirstName = p1.FirstName,
LastName = p1.LastName + ";"+ p2.LastName
}
)).ToList();

foreach (var item in pMergeList)
{
Console.WriteLine("FirstName:" + item.FirstName);
Console.WriteLine("LastName:" + item.LastName);

}

Console.Read();
}


class Person
{
public string FirstName { get; set; }

public string LastName { get; set; }
}


class PersonModel
{
public string FirstName { get; set; }

public string LastNameList { get; set; }
}

csharp-stringintern1

发表于 2015-11-06 | 更新于 2019-05-09

读了深入解析String#intern这篇文章,带着对C#字符串驻留的疑问,在C#里做了如下测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
string s1 = "1";
string s11 = string.Intern(s1);
string s2 = new string(new char[] { '1' });

Console.WriteLine(s1 == s2);//True
Console.WriteLine(ReferenceEquals(s1, s2));//False
Console.WriteLine(Object.ReferenceEquals(string.Intern(s1),s2));//False
Console.WriteLine(Object.ReferenceEquals(s11 ,s2));//False
Console.WriteLine(Object.ReferenceEquals(s1, String.Intern(s2)));//True

String s22 = "2";
String s3 = s22 + "2";
String s4 = "22";
string s5 = string.Intern(s3);

Console.WriteLine(s3 == s4);//true
Console.WriteLine(ReferenceEquals(s3, s4));//false
Console.WriteLine(ReferenceEquals(s5, s4));//true

//让我们来修改下s5的值 看看会不会对s4 造成影响
s5 = "33";
Console.WriteLine(s4);//还是输出22 即使同一个String实例,但是利用任何一个对String实例的引用所进行的修改操作都不会切实地影响到该实例的状态


//不过同样值得注意的是,使用Intern方法让一个字符串存活于驻留池中也有一个副作用:即使已经不存在任何其它引用指向驻留池中的字符串了,这个字符串仍然不一定会被垃圾回收掉。也就是说即使驻留池中的字符串已经没有用处了,它可能也要等到CLR终结时才被销毁。当您使用Intern方法的时候,也应该考虑到这个特殊的行为
Console.Read();

这两篇文章 C#中字符串的内存分配与驻留池, C# 字符串驻留 也有阐述。

jquery-filter-version

发表于 2015-11-04 | 更新于 2019-05-09

jquery1.0 版本的 filter函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//筛选出元素
//1.2.6 如果是Function的话直接grep
//如果不是Function那么调用multiFilter
filter: function(t) {
return this.pushStack(
t.constructor == Array &&
jQuery.map(this,function(a){
for ( var i = 0; i < t.length; i++ )
if ( jQuery.filter(t[i],[a]).r.length )
return a;
}) ||

t.constructor == Boolean &&
( t ? this.get() : [] ) ||

t.constructor == Function &&
jQuery.grep( this, t ) ||
//如果是字符串那么直接调用静态方法filter
jQuery.filter(t,this).r, arguments );
},

我们再看下静态的filter方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
filter: function(t,r,not) {
// Figure out if we're doing regular, or inverse, filtering
var g = not !== false ? jQuery.grep :
function(a,f) {return jQuery.grep(a,f,true);};

while ( t && /^[a-z[({<*:.#]/i.test(t) ) {

var p = jQuery.parse;//调用parse规则数组

for ( var i = 0; i < p.length; i++ ) {
var re = new RegExp( "^" + p[i][0]

// Look for a string-like sequence
.replace( 'S', "([a-z*_-][a-z0-9_-]*)" )

// Look for something (optionally) enclosed with quotes
.replace( 'Q', " *'?\"?([^'\"]*?)'?\"? *" ), "i" );

var m = re.exec( t );

if ( m ) {
// Re-organize the match
if ( p[i][1] )//Match: [@value='test'], [@foo] p[i][1] 为1
m = ["", m[1], m[3], m[2], m[4]];

// Remove what we just matched
t = t.replace( re, "" );

break;
}
}

// :not() is a special case that can be optomized by
// keeping it out of the expression list
if ( m[1] == ":" && m[2] == "not" )
r = jQuery.filter(m[3],r,false).r;

// Otherwise, find the expression to execute
else {
var f = jQuery.expr[m[1]];//访问expr 表达式数组
if ( f.constructor != String )
f = jQuery.expr[m[1]][m[2]];

// Build a custom macro to enclose it
eval("f = function(a,i){" +
( m[1] == "@" ? "z=jQuery.attr(a,m[3]);" : "" ) +
"return " + f + "}");

// Execute it against the current filter
r = g( r, f );//调用grep函数
}
}

// Return an array of filtered elements (r)
// and the modified expression string (t)
return { r: r, t: t };
}

其实要么就是根据filter筛选 ,要么就是调用grep函数

我们来看看grep函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
grep: function(elems, fn, inv) {
// If a string is passed in for the function, make a function
// for it (a handy shortcut)
if ( fn.constructor == String )
fn = new Function("a","i","return " + fn);

var result = [];

// Go through the array, only saving the items
// that pass the validator function
for ( var i = 0; i < elems.length; i++ )
if ( !inv && fn(elems[i],i) || inv && !fn(elems[i],i) )
result.push( elems[i] );

return result;
}

1…434445…47

John

232 日志
43 标签
GitHub Twitter
欢迎关注我的公众号:沉迷Spring
© 2023 johnwonder
由 Hexo 强力驱动 v3.2.0
|
主题 – NexT.Pisces v7.1.1
|
0%