Outils pour utilisateurs

Outils du site


developpement:dotnet:outils:linqpad:my_extensions

ToShortDateTime()

Pour obtenir une date de format YYMMDDHHMM.

public static string ToShortDateTime(this DateTime date)
{
  return date.ToString().Substring(2, 14).Replace("-", "").Replace(":", "").Replace(" ", "");
}

http://geeks.ms/blogs/ohernandez/archive/2008/01/30/executing-arbitrary-queries-in-linq-to-sql.aspx

Fichier complet

void Main()
{
	// Write code to test your extensions here. Press F5 to compile and run.
}
 
public static class MyExtensions
{
 
	#region ---------- JSON Extensions ----------
	//	http://geekswithblogs.net/EltonStoneman/archive/2012/05/11/extension-method-for-outputting-formatted-json-in-linqpad.aspx
	public static object DumpJson(this object value, string description = null)
	{
		return GetJsonDumpTarget(value).Dump(description);
	}
 
	public static object DumpJson(this object value, string description, int depth)
	{
		return GetJsonDumpTarget(value).Dump(description, depth);
	}
 
	public static object DumpJson(this object value, string description, bool toDataGrid)
	{
		return GetJsonDumpTarget(value).Dump(description, toDataGrid);
	}
 
	private static object GetJsonDumpTarget(object value)
	{
		object dumpTarget = value;
		//if this is a string that contains a JSON object, do a round-trip serialization to format it:
		var stringValue = value as string;
		if (stringValue != null)
		{
			if (stringValue.Trim().StartsWith("{"))
			{
				var obj = JsonConvert.DeserializeObject(stringValue);
				dumpTarget = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);
			}
			else
			{
				dumpTarget = stringValue;
			}
		}
		else
		{
			dumpTarget = JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.Indented);
		}
		return dumpTarget;
	}
	#endregion ---------- JSON Extensions ----------
 
	public static string DumpJsonToFile(this object jsonObject, string directory, string filename)
	{
		if (!directory.EndsWith("\\"))
		{
			directory = directory + "\\";
		}
 
		string fullFilename = directory + DateTime.Now.ToShortDateTimeString() + "_" + filename + ".json";
 
		File.WriteAllText(fullFilename, JsonConvert.SerializeObject(jsonObject));
 
		return fullFilename;
	}
 
	public static string ToShortDateTimeString(this DateTime date)
	{
		return date.ToString().Substring(2, 14).Replace("-", "").Replace(":", "").Replace(" ", "");
	}
 
 
	public static IEnumerable<object[]> ExecuteQuery(this DataContext ctx, string query)
	{
		using (DbCommand cmd = ctx.Connection.CreateCommand())
		{
			cmd.CommandText = query;
			ctx.Connection.Open();
			using (DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
				while (rdr.Read())
				{
					object[] res = new object[rdr.FieldCount];
					rdr.GetValues(res);
					yield return res;
				}
			}
		}
	}
 
 
 
	public static IEnumerable ReadFrom(string file)
	{
		string line;
		using (var reader = File.OpenText(file))
		{
			while ((line = reader.ReadLine()) != null)
			{
				yield return line;
			}
		}
	}
 
 
}
}
 
//namespace RapportInspection
//{
 
//	public class TestAClass {
//		public string Name {get; set;}
//		public string Numero {get; set;}
//	}	
 
	public class Utilities
{
	public static string GeneratePassword(int length, int numberOfNonAlphanumericCharacters)
	{
		return System.Web.Security.Membership.GeneratePassword(length, numberOfNonAlphanumericCharacters);
	}
}
//}
 
class EOF {

fichier original

void Main()
{
	// Write code to test your extensions here. Press F5 to compile and run.
}
 
public static class MyExtensions
{
	// Write custom extension methods here. They will be available to all queries.
 
}
 
// You can also define non-static classes, enums, etc.
developpement/dotnet/outils/linqpad/my_extensions.txt · Dernière modification : 2022/02/02 00:42 de 127.0.0.1