using iWareCommon.Utils; using iWareSql.DBModel; using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace iWareUnitTest { [TestClass] public class CSharpTest { [TestMethod] public void Test_String() { //var bb = "缺料原因:失败了啊|DDDADA"; var bb = "DDDADA"; //var bb = ""; //var bb = "缺料原因:失败了啊|"; var queliaoReason = "堆垛机故障"; char splitStr = '|'; var updateRemark = "缺料原因:" + queliaoReason; if (string.IsNullOrEmpty(bb)) { bb = updateRemark + splitStr; } else { //解析字符串 var arr = bb.Split(splitStr); bb = updateRemark + splitStr + arr.Last(); } var zz = bb; } [TestMethod] public void Test_EF_OrderBy() { Task_Main a1 = new Task_Main(); a1.Id = 1; a1.CreateTime = Convert.ToDateTime("2022-05-21 08:55:01.000"); a1.TaskSequence = 55; Task_Main a2 = new Task_Main(); a2.Id = 2; a2.CreateTime = Convert.ToDateTime("2022-05-21 08:55:02.000"); a2.TaskSequence = 55; Task_Main a3 = new Task_Main(); a3.Id = 3; a3.CreateTime = Convert.ToDateTime("2022-05-21 08:56:01.000"); a3.TaskSequence = 55; Task_Main a4 = new Task_Main(); a4.Id = 4; a4.CreateTime = Convert.ToDateTime("2022-05-21 08:56:00.000"); a4.TaskSequence = 51; Task_Main a5 = new Task_Main(); a5.Id = 5; a5.CreateTime = Convert.ToDateTime("2022-05-21 08:56:01.000"); a5.TaskSequence = 57; List mainList = new List(); mainList.Add(a1); mainList.Add(a2); mainList.Add(a3); mainList.Add(a4); mainList.Add(a5); var bb = mainList.OrderBy(x => x.TaskSequence).ThenBy(x => x.CreateTime).ToList(); /* * 证明排序是 先按照TaskSequence排序,如果TaskSequence相等,就按照CreateTime内部小排序。 */ string zz = JsonConvert.SerializeObject(bb); Assert.IsFalse(false); } [TestMethod] public void Test_ExecPercentRetInt() { int i = CSharpHelper.ExecPercentRetInt(22, 24); Assert.IsFalse(false); } [TestMethod] public void TestDictionary() { //C#求获取一个Dictionary中value最小的key Dictionary dict = new Dictionary(); dict.Add("1", 1); dict.Add("2", 2); dict.Add("3", 3); dict.Add("4", 4); dict.Add("5", 1); var minKey = (from d in dict orderby d.Value ascending select d.Key).First(); //考虑到不止一条的情况,分两步查询最小值的所有Key,这样的写法可能更好看懂 var allMinKeys = (from d in dict where d.Value == dict[minKey] select d.Key).ToArray(); var minkey = dict.Keys.Select(x => new { x, y = dict[x] }).OrderBy(x => x.y).First().x; var bb = dict.OrderBy(d => d.Value).Select(d => d.Key).FirstOrDefault(); Assert.IsFalse(false); } [TestMethod] public void TestNull() { int? aa = null; int b = 1 + (int)aa; Assert.IsFalse(false); } [TestMethod] public void TestList() { List sList = new List(); sList.Add("zzz"); sList.Add("888"); string cc = JsonConvert.SerializeObject(sList); } } }