【将特定的ISO8601 TimeSpan(“P2M2W5D”)转换为C#TimeSpan [复制]】教程文章相关的互联网学习教程文章

c# – 将简单的ForEach转换为Linq语句【代码】

我是一个SQL开发人员,所以Linq(和C#一般)对我来说有点陌生,但是我有一个简单的foreach循环,我认为Linq查询可能会更好.即使手头的问题不是特别困难,如果没有别的东西,这将是一个有用的东西.这是我的代码:bool fireAgain = true; foreach (var connMan in Dts.Connections) {Dts.Events.FireInformation( 0, "", String.Format("Connection Manager {0} has connection string {1}", connMan.Name, connMan.ConnectionString), "", ...

c# – 如何将List转换为Dictionary,用null填充值?【代码】

我试图用List作为键来填充一个Dictionary,并将null作为值.我怎样才能实现这一目标?我在C#编程 谢谢.解决方法:您还可以使用Linq Enumerable.ToDictionary,将键和值指定为null:var myList = new List<string> { "first", "second" }; Dictionary<string, string> dict = myList.ToDictionary(item => item, // key item => (string)null // value );请注意,您需要将“null”强制转换为元素类型,否则C#无法推断第二个lambd...

c# – 无法将类型’Newtonsoft.Json.Linq.JObject’转换为复杂类型【代码】

我有json如下,{"H": "Macellum","M": "Receive","A": [{"CustomerId": "172600","OrderId": "69931","OrderStatus": "E0","Buy": "A"}] }和复杂的类型public class OrderStats {public string CustomerId { get; set; }public string OrderId { get; set; }public string OrderStatus { get; set; }public string Buy { get; set; } }我正在尝试铸造如下,dynamic obj = JsonConvert.DeserializeObject<dynamic>(message); OrderSta...

将一些LISP转换为C#【代码】

我正在阅读Paul Graham’s A Plan for Spam,想要更好地理解它,但我的LISP真的很生疏.他有一段代码可以计算概率:(let ((g (* 2 (or (gethash word good) 0)))(b (or (gethash word bad) 0)))(unless (< (+ g b) 5)(max .01(min .99 (float (/ (min 1 (/ b nbad))(+ (min 1 (/ g ngood)) (min 1 (/ b nbad)))))))))我的问题有两个:(1)是否存在将LISP转换为其他语言的Web资源? (我的偏好是基于C的语言)或失败(2)有人可以为我重写...

c# – 将float.MaxValue转换为Long抛出异常?【代码】

试图获取float.MaxValue的整数部分,但它抛出错误?long l = Convert.ToInt64(float.MaxValue);Console.WriteLine(l);错误:算术运算导致溢出.解决方法:float的最大值为3.4E 38,而long的最大值为9.2E 18.因此,float.MaxValue不适合长. 指定大于Int64.MaxValue或小于Int64.MinValue的值时,Convert.ToInt64方法抛出OverflowException. 如果您只想获取float.MaxValue的整数值,请参阅this question的相关答案.但请注意,即使剥离了浮点,浮...

c# – 将List元素转换为String【代码】

在C#中将Int32列表转换为带有’,’之类的分隔符的字符串的最佳方法是什么?解决方法:你可以使用string.Join:var intList = new[] { 1, 2, 3, 4, 5 }; var result = string.Join(",", intList);编辑: 如果您来自.NET 4.0,则string.Join接受输入参数为IEnumerable< T>,因此您无需通过ToArray转换为Array. 但是如果你在.NET 3.5中:和其他答案一样,应该使用ToArray.

c# – 在上传时将图像转换为png【代码】

当用户上传jpg / gif / bmp图像时,我希望将此图像转换为png图像,然后转换为base64字符串. 我一直试图让这个工作,但我真的打了一堵砖墙,有人可以帮我吗? 我目前没有图像转换的代码如下:public ActionResult UploadToBase64String(HttpPostedFileBase file){var binaryData = new Byte[file.InputStream.Length];file.InputStream.Read(binaryData, 0, (int) file.InputStream.Length);file.InputStream.Seek(0, SeekOrigin.Begin)...

将C#字符串转换为JavaScript字符串【代码】

有没有人知道在Asp.net中将C#字符串转换为JavaScript字符串的方法.我的代码看起来像这样:<script>@{string thing = "Cats";}var thing = String(@thing);</script> </div> <body onl oad="eventAlert(thing)"></body>解决方法:您需要JavaScript在编写之前对字符串进行编码,否则您的字符串可能包含导致JavaScript字符串常量过早终止的字符.您可以在System.Web命名空间中使用HttpUtility.JavaScriptStringEncode执行此操作.一旦你完...

c# – 通过使用LINQ跳过空值将Dictionary转换为Dictionary【代码】

我有以下产品类:public class Product {public string Name { get; set; }public float Price { get; set; } public int? CategoryId { get; set; } }现在我必须计算每个CategoryId的产品数量,并将它们放在Dictionary< int,int>中.因此:IQueryable<Product> products = _productServices.GetAll(); //return IQueryable<Product>Dictionary<int, int> productDict = products.ToList().GroupBy(p => p.CategoryId).ToDictio...

C#generics:你能转换ToString吗?【代码】

快点一个?我想转换< T> ToString所以我可以用该类型替换变量? public void ChangePanel<T>(T Action){FieldInfo Field = T.GetField(T.toString);Field.SetValue(SomeObject, Action); }如果有更好的方法,请告诉我! 编辑:FYI SomeObject中的变量与类型具有相同的名称解决方法:您可以使用typeof Keyword获取Type实例,使用Type.Name Property获取类型的名称.public void ChangePanel<T>(T action) {Type typeOfObject = someObject...

c# – 将字符串转换为日期时间dd / MM / yyyy hh:mm:ss tt【代码】

我如何将这个7/3/2015 12:40:02 PM转换为DateTime,格式为“dd / MM / yyyy hh:mm:ss tt”我这样做了:BreackEndTime = DateTime.ParseExact(configViewModel.EndPause, "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);但我总是得到String was not recognized as a valid DateTime.解决方法:由于数月和数天可以使用一位数BreackEndTime = DateTime.ParseExact(configViewModel.EndPause, "d/M/yyyy hh:mm:ss tt", Cult...

c# – 为什么在.net 2.0中向一个原语(即:int)转换null会抛出一个空引用异常而不是一个无效的强制转换异常?【代码】

我正在浏览一些代码并遇到了我的组合框尚未初始化的情况.这是在.NET 2.0中,在下面的代码中,this.cbRegion.SelectedValue为null.int id = (int)this.cbRegion.SelectedValue;此代码抛出了空引用异常,而不是无效的强制转换异常.我想知道是否有人知道为什么它会抛出空引用异常而不是无效的强制转换?解决方法:它与Boxing和拆箱有关.它试图从框中取出一个int(unbox),但该对象为null,因此在获得转换更改之前,您将获得一个空引用异常.

c# – .Net隐式转换指南【代码】

什么是关于何时,应该或不应该定义用户定义的隐式转换的一般指导原则? 我的意思是,例如,“隐式转换永远不会丢失信息”,“隐式转换永远不应该抛出异常”,或“隐式转换永远不应该实例化新对象”.我很确定第一个是正确的,第三个不是(或者我们只能隐式转换为结构),而我不知道第二个.解决方法:第一个并不像你想象的那么简单.这是一个例子:using System;class Test {static void Main(){long firstLong = long.MaxValue - 2;long second...

c#绑定到和接口时向下转换【代码】

是否有更好的方法将基类列表绑定到除了向下转换之外的UI,例如:static void Main(string[] args) {List<Animal> list = new List<Animal>(); Pig p = new Pig(5); Dog d = new Dog("/images/dog1.jpg"); list.Add(p); list.Add(d); foreach (Animal a in list) { DoPigStuff(a as Pig); DoDogStuff(a as Dog); } } static void DoPigStuff(Pig p) {if (p != null) { label1.Text = String.Format("The pigs tail is {...

c# – 将字典转换为List【代码】

我有一个字典< String,Object>我想将它转换为List< Customer>这样做有一个聪明的方法吗?任何例子?谢谢 EDITED 很抱歉没有正确解释.鉴于以下原因,为什么我的结果为0?请注意我试图模拟一个现场情况,第一个键没有意义,并希望排除所以只有我应该得到的客户.为什么不起作用?谢谢你的任何建议class Program {static void Main(string[] args){List<Customer> oldCustomerList = new List<Customer>{new Customer {Name = "Jo1", Surn...

TIME - 相关标签