Length用来描述数组的长度,当前包括string和Byte数组。 默认约定:Code First对string或byte数组的默认长度约定是max。注意:Sql Server Compact中默认最大数组长度是4000。 重写约定:使用HasMaxLength(nn),参数为可空整数。
1
Property(t => t.Name).HasMaxLength(50);
另外关于Length的Fluent API还有下面2个:
IsFixedLength(),配置属性为固定长度。
IsMaxLength(),配置属性为数据库提供程序允许的最大长度。
1.4.2. 配置Data Type
Data Type表示将.NET类型映射到的数据库的数据类型。 默认约定:列的数据类型由使用的数据库提供程序决定。以SQL Server为例:String->nvarchar(max),Integer->int,Byte[]->varbinary(max),Boolean->bit。 重写约定:使用HasColumnType(“xxx”),下面列子的Photo是byte[]类型,配置映射到image数据类型:
//实体 public class Trip { public Guid Identifier { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public decimal CostUSD { get; set; } public string Description { get; set; } public byte[] RowVersion { get; set; } }
//复杂类型 public class Address { public int AddressId { get; set; } public string StreetAddress { get; set; } public string City { get; set; } public string State { get; set; } public string ZipCode { get; set; } }
//复杂类型 public class PersonalInfo { public Measurement Weight { get; set; } public Measurement Height { get; set; } public string DietryRestrictions { get; set; } }
//复杂类型 public class Measurement { public decimal Reading { get; set; } public string Units { get; set; } }
//实体 public class Person { public Person() { Address = new Address(); Info = new PersonalInfo() { Weight = new Measurement(), Height = new Measurement() }; }
public int PersonId { get; set; } public int SocialSecurityNumber { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public Address Address { get; set; } public byte[] Photo { get; set; } public PersonalInfo Info { get; set; } public byte[] RowVersion { get; set; } }
//转换成大写人民币 var digitUppercase = function (n) { var fraction = ['角', '分']; var digit = [ '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' ]; var unit = [ ['元', '万', '亿'], ['', '拾', '佰', '仟'] ]; var head = n < 0 ? '欠' : ''; n = Math.abs(n); var s = ''; for (var i = 0; i < fraction.length; i++) { s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, ''); } s = s || '整'; n = Math.floor(n); for (var i = 0; i < unit[0].length && n > 0; i++) { var p = ''; for (var j = 0; j < unit[1].length && n > 0; j++) { p = digit[n % 10] + unit[1][j] + p; n = Math.floor(n / 10); } s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s; } return head + s.replace(/(零.)*零元/, '元') .replace(/(零.)+/g, '零') .replace(/^整$/, '零元整'); }
3. 获取两个List或数组的差集与交集
3.1. 差集
1 2 3 4 5 6 7 8 9 10 11 12 13 14
List<int> list1 = new List<int>(); list1.Add(1); list1.Add(2); list1.Add(3); List<int> list2 = new List<int>(); list2.Add(3); list2.Add(4); list2.Add(5); //得到的结果是4,5 即减去了相同的元素。 List<int> list3 = list2.Except(list1).ToList(); foreach (int i in list3) { MessageBox.Show(i.ToString()); }
3.2. 合并两个数组,并去掉重复元素,然后排序(C#)
1 2 3 4 5 6 7
List<int> numbers1 = new List<int>() { 5, 4, 1, 3, 9, 8, 6, 7, 12, 10 }; List<int> numbers2 = new List<int>() { 15, 14, 11, 13, 19, 18, 16, 17, 12, 10 }; var newQuerty = numbers1.Concat( from n in numbers2 where !numbers1.Contains(n) select n ).OrderBy(n=>n);
3.3. 合并两个数组,并去除合并后的重复数据, 并排序
1 2 3 4
int[] A={1,2,2,3,4,5,6,6,6}; int[] B={2,2,2,3,7,8,9,5}; List<int> list = new List<int>(A); list.AddRange(B);
4. List<父类>引用List<子类>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public class Father {
} public class Son:Father { }
public class Test { public void Test() { List<Father> fathers = new List<Father>(); List<Son> sons = new List<Sons>(); sons = sons.Cast<Father>().ToList(); } }