Outils pour utilisateurs

Outils du site


developpement:dotnet:outils:linqpad:my_extensions

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
developpement:dotnet:outils:linqpad:my_extensions [2015/05/21 21:46] sgariepydeveloppement:dotnet:outils:linqpad:my_extensions [2022/02/02 00:42] (Version actuelle) – modification externe 127.0.0.1
Ligne 13: Ligne 13:
  
 http://geeks.ms/blogs/ohernandez/archive/2008/01/30/executing-arbitrary-queries-in-linq-to-sql.aspx http://geeks.ms/blogs/ohernandez/archive/2008/01/30/executing-arbitrary-queries-in-linq-to-sql.aspx
 +
 +
 +====== Fichier complet ======
 +
 +<code csharp>
 +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 {
 +</code>
 +
 +
 +====== fichier original ======
 +
 +
 +<code csharp>
 +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.
 +
 +</code>
 +
 +
 +
 +
 +
developpement/dotnet/outils/linqpad/my_extensions.1432237606.txt.gz · Dernière modification : 2022/02/02 00:43 (modification externe)