123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace LYFZ
- {
- public static class LinQExtensions
- {
- public static string ToYYYY_MM_dd_HH_mm_ss(this DateTime dtime)
- {
- return dtime.ToString("yyyy-MM-dd HH:mm:ss");
- }
- public static string ToYYYY_MM_dd(this DateTime dtime)
- {
- return dtime.ToString("yyyy-MM-dd");
- }
- public static string ToHH_mm_ss(this DateTime dtime)
- {
- return dtime.ToString("HH:mm:ss");
- }
- /// <summary>
- /// 转对象为字符串 如果对象为null 返加 空字符串
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string ToString_s(this object obj)
- {
- if (obj != null)
- {
- return obj.ToString();
- }
- else {
- return "";
- }
- }
- /// <summary>
- /// 转为小写字符串
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string ToLowerString(this object obj)
- {
- return obj.ToString().ToLower();
- }
- /// <summary>
- /// 转为去除前后空格的小写字符串
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string ToTrimLowerString(this object obj)
- {
- return obj.ToString().ToLower().Trim();
- }
- /// <summary>
- /// 转为大写字符串
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string ToUpperString(this object obj)
- {
- return obj.ToString().ToUpper();
- }
- /// <summary>
- /// 转为去除前后空格的大写字符串
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string ToTrimUpperString(this object obj)
- {
- return obj.ToString().ToUpper().Trim();
- }
- /// <summary>
- /// 通过使用默认的相等比较器对值进行比较返回序列中的非重复元素。
- /// </summary>
- /// <typeparam name="TSource"></typeparam>
- /// <typeparam name="TKey"></typeparam>
- /// <param name="source"></param>
- /// <param name="keySelector"></param>
- /// <returns></returns>
- public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- {
- HashSet<TKey> seenKeys = new HashSet<TKey>();
- foreach (TSource element in source)
- {
- if (seenKeys.Add(keySelector(element)))
- {
- yield return element;
- }
- }
- }
- /// <summary>
- /// 转 布尔值为 int
- /// </summary>
- /// <param name="bl"></param>
- /// <returns></returns>
- public static int BoolToInt(this bool bl)
- {
- return bl ? 1 : 0;
- }
- /// <summary>
- /// 转 int 为 布尔值
- /// </summary>
- /// <param name="i"></param>
- /// <returns></returns>
- public static bool IntToBool(this int i)
- {
- return i > 0;
- }
- /// <summary>
- /// DateTime转换为JavaScript时间戳 13位数长度
- /// </summary>
- /// <param name="t"></param>
- /// <returns></returns>
- public static long ToJavaScriptTimeStamp(this DateTime t)
- {
- return LYFZ.WinAPI.TimeStampConversion.DateTimeToJavaScriptTimeStamp(t);
- }
- /// <summary>
- /// JavaScript时间戳转换为DateTime
- /// </summary>
- /// <param name="timeStamp">为有效的时间戳整数</param>
- /// <returns></returns>
- public static DateTime JavaScriptTimeStampToDateTime(this long timeStamp)
- {
- try
- {
- return LYFZ.WinAPI.TimeStampConversion.JavaScriptTimeStampToDateTime(timeStamp);
- }
- catch (Exception ex)
- {
- throw new Exception("时间戳转换时出错,时间戳无效:" + ex.Message);
- }
- }
- /// <summary>
- /// 序列化object到byte[]
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static byte[] ToSerializerRetBytes(this object o)
- {
- if (o != null)
- {
- return LYFZ.WinAPI.DataSetSerializerDeserialize.ObjectSerializerRetBytes(o);
- }
- else
- {
- return null;
- }
- }
- }
- }
|