C#NPOI.RabbitMQ.EF.Attribute.HttpRuntime.Cache.AD域.List根据指定字段去重.前端JQuery.Cache.I18N(多语言).data-xx(自定义属性)
使用NPOI 操作Excel
个人使用的电脑基本默认安装Excel 操作起来
调用Excel的组件便可.如果是一台服务器.没有安装Excel,也就无法调用Excel组件.
在此推荐第三方插件.NPOI 支持XLS(2007)和XLSX(2012)读写.
using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.IO; namespace WebApplication1.Helper { public class ExcelHelper : IDisposable { private string fileName = null; //文件名 private IWorkbook workbook = null; private FileStream fs = null; private bool disposed; public ExcelHelper(string fileName) { this.fileName = fileName; disposed = false; fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); if (fileName.IndexOf(".xlsx") > 0) // 2007版本 workbook = new XSSFWorkbook(fs); else if (fileName.IndexOf(".xls") > 0) // 2003版本 workbook = new HSSFWorkbook(fs); } public List<string> SheetName { get { List<string> data = null; if (workbook != null) { data = new List<string>(); for (int i = 0; i < workbook.NumberOfSheets; i++) { data.Add(workbook.GetSheetAt(i).SheetName.ToString()); } } return data; } } public int SheetCount { get { return workbook == null ? 0 : workbook.NumberOfSheets; } } /// <summary> /// 将excel中的数据导入到DataTable中 /// </summary> /// <param name="sheetName">excel工作薄sheet的名称</param> /// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param> /// <returns>返回的DataTable</returns> public DataTable ExcelToDataTable(string sheetName, bool isFirstRowColumn) { ISheet sheet = null; DataTable data = new DataTable(); int startRow = 0; try { //fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); //if (fileName.IndexOf(".xlsx") > 0) // 2007版本 // workbook = new XSSFWorkbook(fs); //else if (fileName.IndexOf(".xls") > 0) // 2003版本 // workbook = new HSSFWorkbook(fs); if (sheetName != null) { sheet = workbook.GetSheet(sheetName); if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet { sheet = workbook.GetSheetAt(0); } } else { sheet = workbook.GetSheetAt(0); } if (sheet != null) { IRow firstRow = sheet.GetRow(0); int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数 if (isFirstRowColumn) { for (int i = firstRow.FirstCellNum; i < cellCount; ++i) { ICell cell = firstRow.GetCell(i); if (cell != null) { string cellValue = cell.StringCellValue; if (cellValue != null) { DataColumn column = new DataColumn(cellValue); data.Columns.Add(column); } } } startRow = sheet.FirstRowNum + 1; } else { startRow = sheet.FirstRowNum; } //最后一列的标号 int rowCount = sheet.LastRowNum; for (int i = startRow; i <= rowCount; ++i) { IRow row = sheet.GetRow(i); if (row == null) continue; //没有数据的行默认是null DataRow dataRow = data.NewRow(); for (int j = row.FirstCellNum; j < cellCount; ++j) { if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null dataRow[j] = row.GetCell(j).ToString(); } data.Rows.Add(dataRow); } } return data; } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); return null; } } public static void CreateExcel(string projectName) { try { string fileName = $"{projectName}.xlsx"; // 文件名称 string filePath = $"{ConfigurationManager.AppSettings["file"].ToString()}" + "\\" + fileName; // 2.解析单元格头部,设置单元头的中文名称 XSSFWorkbook workbook = new XSSFWorkbook(); // 工作簿 ISheet sheet = workbook.CreateSheet("sheet"); //#region 设置Excel表格第一行的样式 //IRow titleInfo = sheet.CreateRow(0); //ICell cellTitle = titleInfo.CreateCell(0); //cellTitle.SetCellValue("会员信息批量录入模板"); //ICellStyle titleStyle = workbook.CreateCellStyle(); //IFont titleFont = workbook.CreateFont(); //titleFont.FontHeightInPoints = 25; //titleFont.Boldweight = short.MaxValue;//字体加粗 //titleStyle.SetFont(titleFont); //cellTitle.CellStyle = titleStyle; //#endregion //IRow dataFields = sheet.CreateRow(2); //ICellStyle style = workbook.CreateCellStyle();//创建样式对象 //style.Alignment = HorizontalAlignment.CENTER;//水平对齐 //style.VerticalAlignment = VerticalAlignment.CENTER;//垂直对齐 //IFont font = workbook.CreateFont(); //创建一个字体样式对象 //font.FontName = "宋体"; //和excel里面的字体对应 //font.Color = new HSSFColor.RED().GetIndex();//颜色参考NPOI的颜色对照表(替换掉PINK()) //font.FontHeightInPoints = 10;//字体大小 //font.Boldweight = short.MaxValue;//字体加粗 //style.SetFont(font); //将字体样式赋给样式对象 //sheet.SetColumnWidth(0, 20 * 256);//设置列宽 //string[] colums = { "*会员卡号", "*会员手机", "*会员姓名", "*会员等级", "会员性别", "电子邮箱", "会员状态", "固定电话", "永久有效", "身份证号", "开卡费用", "会员地址", "备注信息" }; //ICell Cell = null; //for (int i = 0; i < colums.Count(); i++) //{ // Cell = dataFields.CreateCell(i); // Cell.CellStyle = style; // Cell.SetCellValue(colums[i]); // sheet.SetColumnWidth(i, 17 * 256); //} //sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, colums.Count()));//合并单元格 // 4.生成文件 FileStream file = new FileStream(filePath, FileMode.Create); workbook.Write(file); file.Close(); } catch (Exception ex) { throw ex; } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { if (fs != null) fs.Close(); } fs = null; disposed = true; } } } }
读:
using (ExcelHelper excelHelper = new ExcelHelper(ConfigurationManager.AppSettings["ExcelFile"])) { var dataTable = excelHelper.ExcelToDataTable(null, true); }
ConfigurationManager.AppSettings["ExcelFile"]) 指文件位置 ExcelToDataTable(null, true); sheetName指Excel中每个Sheet的名字 isFirstRowColumn指第一行是否是列名
写:
ExcelHelper.CreateExcel(projectName); FileStream fs = new FileStream($"{ConfigurationManager.AppSettings["file"].ToString()}\\{projectName}.xlsx", FileMode.Open, FileAccess.Read); IWorkbook workbook = new XSSFWorkbook(fs); for (int i = 0; i < Sheet.Count; i++) { var Srarch = data.Where(a => a.Sheet == Sheet[i]).ToList(); DataTable dataTable = new DataTable(); dataTable.Columns.Add("Template"); dataTable.Columns.Add("Code"); dataTable.Columns.Add("ZH_CN"); dataTable.Columns.Add("Description"); dataTable.Columns.Add("EN_US"); dataTable.Columns.Add("ZH_TW"); dataTable.Columns.Add("KO"); dataTable.Columns.Add("ZH_HK"); for (int a = 0; a < Srarch.Count; a++) { DataRow row = dataTable.NewRow(); row[0] = Srarch[a].Template; row[1] = Srarch[a].Code; row[2] = Srarch[a].ZH_CN; row[3] = Srarch[a].Description; row[4] = Srarch[a].EN_US; row[5] = Srarch[a].ZH_TW; row[6] = Srarch[a].KO; row[7] = Srarch[a].ZH_HK; dataTable.Rows.Add(row); } dataTable.TableName = Sheet[i]; ISheet sheet = workbook.CreateSheet(dataTable.TableName); //表头 IRow row2 = sheet.CreateRow(0); for (int a = 0; a < dataTable.Columns.Count; a++) { ICell cell = row2.CreateCell(a); cell.SetCellValue(dataTable.Columns[a].ColumnName); } //数据 for (int a = 0; a < dataTable.Rows.Count; a++) { IRow row1 = sheet.CreateRow(a + 1); for (int j = 0; j < dataTable.Columns.Count; j++) { ICell cell = row1.CreateCell(j); cell.SetCellValue(dataTable.Rows[a][j].ToString()); } } } MemoryStream stream = new MemoryStream(); workbook.Write(stream); var buf = stream.ToArray(); //保存为Excel文件 using (FileStream fs1 = new FileStream($"{ConfigurationManager.AppSettings["file"].ToString()}\\{projectName}.xlsx", FileMode.Open, FileAccess.Write)) { fs1.Write(buf, 0, buf.Length); fs1.Flush(); }
ExcelHelper.CreateExcel(projectName);
创建Excel ,projectName 是Excel名字,路径默认是解决方案下File文件夹
两个For循环,第一个循环是创建列名,第二个循环是创建列下字段的内容
当然这只是创建Datatable
所以后面还有两个循环 将每个TABLE中列名放到Excel中的Sheet.以及内容
最后fs1.Write写入Excel
RabbitMQ
是一种应用程序对应用程序的通信方法
举个例子
前端站点向服务站点发起请求。一般服务站点要考虑到并发和数据总量保持运行稳定.
如果前端的请求发送到Rabbit并做持久化.服务端隔段时间批量响应请求。就大大减少了服务站点的压力
接下来的栗子就是A站点向Rabbit发消息并走入队列.然后B站点拉取消息并回应,至于隔多久取,一次取多少条.这部分就没做限制(计数就好).
下面是A站点
ConnectionFactory factory = new ConnectionFactory(); factory.HostName = "127.0.0.1"; //默认端口 factory.Port = 5672; factory.UserName = "guest";//用户名 factory.Password = "guest";//密码 using (IConnection conn = factory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { //在MQ上定义一个持久化队列,如果名称相同不会重复创建 channel.QueueDeclare("MQTest", true, false, false, null); while (true) { string message = string.Format("Message_{0}", Console.ReadLine()); byte[] buffer = Encoding.UTF8.GetBytes(message); IBasicProperties properties = channel.CreateBasicProperties(); properties.DeliveryMode = 2; channel.BasicPublish("", "MQTest", properties, buffer); Console.WriteLine("消息发送成功:" + message); } } }
消息在Rabbit中.A接触Rabbit,B接触Rabbit.
最终是AB交互.但是过程相互不接触
ConnectionFactory factory = new ConnectionFactory(); factory.HostName = "127.0.0.1"; //默认端口 factory.Port = 5672; factory.UserName = "guest";//用户名 factory.Password = "guest";//密码 using (IConnection conn = factory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { //在MQ上定义一个持久化队列,如果名称相同不会重复创建 channel.QueueDeclare("MQTest", true, false, false, null); //输入1,那如果接收一个消息,但是没有应答,则客户端不会收到下一个消息 channel.BasicQos(0, 1, false); Console.WriteLine("Listening..."); //在队列上定义一个消费者 QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel); //消费队列,并设置应答模式为程序主动应答 channel.BasicConsume("MQTest", false, consumer); while (true) { //阻塞函数,获取队列中的消息 BasicDeliverEventArgs ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue(); byte[] bytes = ea.Body; string str = Encoding.UTF8.GetString(bytes); Console.WriteLine("队列消息:" + str.ToString()); //回复确认 channel.BasicAck(ea.DeliveryTag, false); } } }
EF
像这个被大家用的滚瓜烂熟的框架.我这会拿出来说似乎有点小儿科.不过我就是要写(还写得贼简单)
Nuget中引入EF
自定义DbContext继承DbContext
public class HotelDbContext : DbContext { public HotelDbContext() : base("name=ConnCodeFirst") { Database.SetInitializer(new MigrateDatabaseToLatestVersion<HotelDbContext, Configuration>("ConnCodeFirst")); } public DbSet<TSM_Admin1111111111111> TSM_Admin { get; set; } }
ConnCodeFirst 是数据库连接字符串
Configuration 如下
internal sealed class Configuration : DbMigrationsConfiguration<HotelDbContext> { public Configuration() { // 自动迁移 TRUE code frist AutomaticMigrationsEnabled = true; } protected override void Seed(HotelDbContext context) { context.TSM_Admin.AddOrUpdate( p => p.UserID, new TSM_Admin1111111111111 { UserID = "1", UserName = "111" }, new TSM_Admin1111111111111 { UserID = "2", UserName = "222" }, new TSM_Admin1111111111111 { UserID = "3", UserName = "333" } ); } }
AutomaticMigrationsEnabled 意思就是找不到数据库的时候自动创建数据库。顺带还会创建初始数据(表和数据)。在Seed中
说真的很鸡肋.用的很想骂街
接下来是EF的各种使用方式。。婴儿教学版
new Configuration(); using (var data = new HotelDbContext()) { //查询.全表 var a = data.TSM_Admin.ToList(); //插入 data.TSM_Admin.Add(new TSM_Admin1111111111111 { UserID = "高1030测试" }); data.SaveChanges(); //修改 var model = data.TSM_Admin.Where(b => b.UserID == "高1030测试").First(); if (model != null) { model.UserName = "高1030测试名字"; data.Entry<TSM_Admin1111111111111>(model).State = System.Data.Entity.EntityState.Modified; data.SaveChanges(); } //删除 var delete = data.TSM_Admin.Where(b => b.UserID == "高1030测试").First(); if (delete != null) { var result = data.TSM_Admin.Where(b => b.UserID == delete.UserID).FirstOrDefault(); data.TSM_Admin.Remove(result); data.SaveChanges(); } var select = data.Database.SqlQuery<TSM_Admin1111111111111>("select * from tsm_123 ").ToList(); var insert = data.Database.ExecuteSqlCommand("insert into TSM_Admin(UserID) values('高1030测试')"); }
能用Dapper。就别用EF..
Attribute。
很多人都梦想着自定义属于自己的属性.那么
我用双手成就你的梦想
StringLength是自定义属性的名字Attribute是必须加上去的。并且在使用中会被忽视
[AttributeUsage(AttributeTargets.Property)] public class StringLengthAttribute : Attribute { private int _maximumLength; public StringLengthAttribute(int maximumLength) { _maximumLength = maximumLength; } public int MaximumLength { get { return _maximumLength; } } }
接下来定义使用的实体
public class People { [StringLength(8)] public string Name { get; set; } [StringLength(15)] public string Description { get; set; } }
接下来定义获取属性内容的方法
public class ValidationModel { public void Validate(object obj) { var t = obj.GetType(); //由于我们只在Property设置了Attibute,所以先获取Property var properties = t.GetProperties(); foreach (var property in properties) { //这里只做一个stringlength的验证,这里如果要做很多验证,需要好好设计一下,千万不要用if elseif去链接 //会非常难于维护,类似这样的开源项目很多,有兴趣可以去看源码。 if (!property.IsDefined(typeof(StringLengthAttribute), false)) continue; var attributes = property.GetCustomAttributes(false); foreach (var attribute in attributes) { //这里的MaximumLength 最好用常量去做 var maxinumLength = (int)attribute.GetType(). GetProperty("MaximumLength"). GetValue(attribute); //获取属性的值 var propertyValue = property.GetValue(obj) as string; if (propertyValue == null) throw new Exception("exception info");//这里可以自定义,也可以用具体系统异常类 if (propertyValue.Length > maxinumLength) throw new Exception(string.Format("属性{0}的值{1}的长度超过了{2}", property.Name, propertyValue, maxinumLength)); } } } }
接下来是调用过程
var people = new People() { Name = "qweasdzxcasdqweasdzxc", Description = "description" }; try { new ValidationModel().Validate(people); } catch (Exception ex) { Console.WriteLine(ex.Message); }
这个段子充分解释了如何使用自定义Attribute
核心思想是万物皆可supreme
HttpRuntime.Cache
开发中很多不常改动的数据。可以考虑将数据从数据库里取出来后的指定时间内存在本地内存中
using System; using System.Web; using System.Web.Caching; namespace Chinamoney.Core { public class CacheHelper { /// <summary> /// 获取数据缓存 /// </summary> /// <param name="cacheKey">键</param> public static object GetCache(string cacheKey) { var objCache = HttpRuntime.Cache.Get(cacheKey); return objCache; } /// <summary> /// 设置数据缓存 /// </summary> public static void SetCache(string cacheKey, object objObject) { var objCache = HttpRuntime.Cache; objCache.Insert(cacheKey, objObject); } /// <summary> /// 设置数据缓存 /// </summary> public static void SetCache(string cacheKey, object objObject, int Hours) { try { if (objObject == null) return; var objCache = HttpRuntime.Cache; //相对过期 //objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null); //绝对过期时间 objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddHours(Hours), TimeSpan.Zero, CacheItemPriority.High, null); } catch (Exception e) { Log.Logger.Log("缓存写入异常:",e); } } /// <summary> /// 移除指定数据缓存 /// </summary> public static void RemoveAllCache(string cacheKey) { var cache = HttpRuntime.Cache; cache.Remove(cacheKey); } /// <summary> /// 移除全部缓存 /// </summary> public static void RemoveAllCache() { var cache = HttpRuntime.Cache; var cacheEnum = cache.GetEnumerator(); while (cacheEnum.MoveNext()) { cache.Remove(cacheEnum.Key.ToString()); } } } }
读取
CacheHelper.GetCache("cacheExtraterritorial") as List<ExtraterritorialDataEntity>;
写入
CacheHelper.SetCache("cacheExtraterritorial", enumerable, result.Count);
清空全部缓存(带名称清理指定缓存,不带名称清理全部缓存)
CacheHelper.RemoveAllCache("cacheExtraterritorial");
AD是微软推出的又一款对象数据库,
集成了权限和员工信息。
在此贴出AD操作的Helper
using Chinamoney.Core.Log; using System; using System.Collections; using System.DirectoryServices; namespace Chinamoney.Core { /// <summary> /// AD域 /// </summary> public class ADHelper { /// <summary> /// 获取单个用户 /// </summary> /// <param name="user"></param> /// <returns></returns> public static DirectoryEntry GetOne(string ldap, string user, string password, string search) { try { DirectoryEntry entry = new DirectoryEntry(ldap, user, password); DirectorySearcher searcher = new DirectorySearcher(entry); //searcher.Filter = "(&(objectClass=user)(SAMAccountName=" + search + "))";//不支持中文名.改成全局搜索. searcher.Filter = "(&(objectCategory=person)(objectClass=user))"; SearchResultCollection result = searcher.FindAll(); foreach (SearchResult item in result) { ICollection serach = item.Properties.PropertyNames; foreach (var model in serach) { if (model.ToString() == "name")//邮件中文名 AD Name { if (item.Properties["name"][0].ToString() == search) { return item.GetDirectoryEntry(); } } } } entry.Close(); //return null; } catch (Exception e) { Logger.Log("AD同步异常信息.异常信息:", e); } return null; } /// <summary> /// 获取全部用户 /// </summary> /// <param name="ldap"></param> /// <param name="user"></param> /// <param name="password"></param> /// <returns></returns> public static SearchResultCollection GetALL(string ldap, string user, string password) { try { using (DirectoryEntry objDE = new DirectoryEntry(ldap, user, password)) { string strFilter = "(&(objectCategory=person)(objectClass=user))"; using (DirectorySearcher objSearcher = new DirectorySearcher(objDE, strFilter)) { // strFilter = "(objectGUID=*)"; // DirectorySearcher objSearcher = new DirectorySearcher(objDE, strFilter); return objSearcher.FindAll(); } } //DirectoryEntry objDE = new DirectoryEntry(ldap, user, password);//("LDAP://cftes.org", @"cftes\admin", "Pass@123"); } catch (Exception e) { Logger.Log("AD同步异常信息.异常信息:", e); } return null; } /// <summary> /// 禁用指定用户 /// </summary> /// <param name="entry"></param> public static void DisableUser(DirectoryEntry entry) { if (entry != null) { using (DirectoryEntry de = entry) { int control = (int)(de.Properties["userAccountControl"][0]); control = control | (int)(ADS_USER_FLAG_ENUM.ADS_UF_ACCOUNTDISABLE); de.Properties["userAccountControl"].Value = control; de.CommitChanges(); } } } /// <summary> /// 用户属性定义标志 /// </summary> [Flags] public enum ADS_USER_FLAG_ENUM : int { /// <summary> /// 登录脚本标志。如果通过 ADSI LDAP 进行读或写操作时,该标志失效。如果通过 ADSI WINNT,该标志为只读。 /// </summary> ADS_UF_SCRIPT = 0X0001, /// <summary> /// 用户帐号禁用标志 /// </summary> ADS_UF_ACCOUNTDISABLE = 0X0002, /// <summary> /// 主文件夹标志 /// </summary> ADS_UF_HOMEDIR_REQUIRED = 0X0008, /// <summary> /// 过期标志 /// </summary> ADS_UF_LOCKOUT = 0X0010, /// <summary> /// 用户密码不是必须的 /// </summary> ADS_UF_PASSWD_NOTREQD = 0X0020, /// <summary> /// 密码不能更改标志 /// </summary> ADS_UF_PASSWD_CANT_CHANGE = 0X0040, /// <summary> /// 使用可逆的加密保存密码 /// </summary> ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 0X0080, /// <summary> /// 本地帐号标志 /// </summary> ADS_UF_TEMP_DUPLICATE_ACCOUNT = 0X0100, /// <summary> /// 普通用户的默认帐号类型 /// </summary> ADS_UF_NORMAL_ACCOUNT = 0X0200, /// <summary> /// 跨域的信任帐号标志 /// </summary> ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = 0X0800, /// <summary> /// 工作站信任帐号标志 /// </summary> ADS_UF_WORKSTATION_TRUST_ACCOUNT = 0x1000, /// <summary> /// 服务器信任帐号标志 /// </summary> ADS_UF_SERVER_TRUST_ACCOUNT = 0X2000, /// <summary> /// 密码永不过期标志 /// </summary> ADS_UF_DONT_EXPIRE_PASSWD = 0X10000, /// <summary> /// MNS 帐号标志 /// </summary> ADS_UF_MNS_LOGON_ACCOUNT = 0X20000, /// <summary> /// 交互式登录必须使用智能卡 /// </summary> ADS_UF_SMARTCARD_REQUIRED = 0X40000, /// <summary> /// 当设置该标志时,服务帐号(用户或计算机帐号)将通过 Kerberos 委托信任 /// </summary> ADS_UF_TRUSTED_FOR_DELEGATION = 0X80000, /// <summary> /// 当设置该标志时,即使服务帐号是通过 Kerberos 委托信任的,敏感帐号不能被委托 /// </summary> ADS_UF_NOT_DELEGATED = 0X100000, /// <summary> /// 此帐号需要 DES 加密类型 /// </summary> ADS_UF_USE_DES_KEY_ONLY = 0X200000, /// <summary> /// 不要进行 Kerberos 预身份验证 /// </summary> ADS_UF_DONT_REQUIRE_PREAUTH = 0X4000000, /// <summary> /// 用户密码过期标志 /// </summary> ADS_UF_PASSWORD_EXPIRED = 0X800000, /// <summary> /// 用户帐号可委托标志 /// </summary> ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 0X1000000 } } }
调用方式
SearchResultCollection data = ADHelper.GetALL(ldap, @name, password);
禁用账号
ADHelper.DisableUser(ADHelper.GetOne(ldap, name, password, serach.ChinaName));
List<T>根据指定字段去重
List<string>去重很容易Distinct就可以
如果是List<T>在实体中根据指定字段判断重复项.如果每次都写循环是不是很累.
在此贴出帮助类
using System.Collections.Generic; namespace Chinamoney.Core { /// <summary> /// 去除重复项 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public delegate bool EqualsComparer<T>(T x, T y); public class ComparerHelper<T> : IEqualityComparer<T> { private EqualsComparer<T> _equalsComparer; public ComparerHelper(EqualsComparer<T> equalsComparer) { this._equalsComparer = equalsComparer; } public bool Equals(T x, T y) { if (null != this._equalsComparer) return this._equalsComparer(x, y); else return false; } public int GetHashCode(T obj) { return obj.ToString().GetHashCode(); } } }
调用方式
rows = rows.Distinct(new ComparerHelper<ExtraterritorialDataDto>((x, y) => (null != x && null != y) && (x.senderaddress == y.senderaddress))).ToList();
拉姆达指定的字段就是不能相同的字段,可以根据情景变化
JqueryCache
Cookie有长度限制4m,
如果真的有大量数据要存放在客户端Jquery.Cache 是个不错的考虑
<script src="/webStorageCache/web-storage-cache.js"></script> <script src="/webStorageCache/ajax-cache.js"></script> var wsCache = new WebStorageCache();//缓存对象.全局通用
wsCache.isSupported() //当前浏览器是否支持缓存 wsCache.clear();//清空所有缓存 wsCache.set('键', 值, { exp: 时间.单位秒。整数 });
wsCache.get('键')
缓存插入成功后可以再浏览器中看到(开发者工具)
I18N
<script src="~/Scripts/jquery-3.3.1.js"></script> <script src="~/Scripts/jquery.json.min.js"></script> <script src="~/Scripts/jquery.i18n.properties.js"></script>
HTML
<div>
<p id=”text”></p>
<button onclick=”switchLang()” id=”lang”></button>
</div>
SCRIPT
<script>
var LANGUAGE_CODE = “en_US”; //标识语言
function loadProperties(type) {
jQuery.i18n.properties({
name: ‘strings’, // 资源文件名称
path: ‘static/’, // 资源文件所在目录路径
mode: ‘map’, // 模式:变量或 Map
language: type, // 对应的语言
cache: false,
encoding: ‘UTF-8’,
callback: function () { // 回调方法
$(‘#text’).html($.i18n.prop(‘string_text’));
$(‘#lang’).html($.i18n.prop(‘string_lang’));
}
});
}
function switchLang() {
LANGUAGE_CODE = LANGUAGE_CODE == ‘zh_CN’ ? ‘en_US’ : ‘zh_CN’;
loadProperties(LANGUAGE_CODE);
}
$(document).ready(function () {
LANGUAGE_CODE = jQuery.i18n.normaliseLanguageCode({}); //获取浏览器的语言
loadProperties(LANGUAGE_CODE);
})
</script>
来一顿专业的BB压压惊
在HTML中需要设置多国语言的元素上添加ID
properties.name+ LANGUAGE_CODE 就是当前加载的语种文件
如果需要改变语言的话
赋值 LANGUAGE_CODE
重新加载 loadProperties(LANGUAGE_CODE);
页面无刷新喔
完美
HTML 自定义属性 DATA-XX
HTML允许我们自定义的属性.都以DATA-开头
举个栗子
再JS中或者直接写HTML
html += "<li class='cl' dataurl='" + data[i].Content + "' datatitle='" + data[i].Title + "' datadesc='" + data[i].Describe + "' time='" + data[i].ShowTime + "'>"; $('#courseData').append($(html));
栗子中是dataurl,datatitle,datadesc
dataurl或者data-url并没有什么区别。
因为data-xx 浏览器会认为是用户自定义属性。不做任何处理
至于dataxx浏览器根本不认识。所以也不做任何处理
大概就是我开心就好的意思
读 var dataurl = $(this).attr("dataurl");
写 $("div").attr("data-my-value", "使用attr改变的数据");