我有一个阵列
int[] Values = new int[] { 5, 43, 45, 25, 16, 89, 65, 36, 62 };
Run Code Online (Sandbox Code Playgroud)
目前我正在计算所有值之间的最大距离 84 = 89 - 5
int MaxDistance = Values.SelectMany((a) => Values.Select((b) => Math.Abs(a - b))).Max();
Run Code Online (Sandbox Code Playgroud)
现在我想计算最小距离 2 = 45 - 43
@ ycsun的意见 - 这不起作用
int MinDistancee = Values.SelectMany((ia, a) => Values.Select((ib, b) => ib == ia ? int.MaxValue : Math.Abs(a - b))).Min();
Run Code Online (Sandbox Code Playgroud) 我有一个字符串
string value = "123456789";
Run Code Online (Sandbox Code Playgroud)
现在我需要按以下方式重新排列字符串:
123456789
1 right
12 left
312 right
3124 left
53124 right
...
975312468 result
Run Code Online (Sandbox Code Playgroud)
是否有一个花哨的linq one liner解决方案来解决这个问题?
我目前的(工作但不太好看)解决方案:
string items = "abcdefgij";
string result = string.Empty;
for (int i = 0; i < items.Length; i++)
{
if (i % 2 != 0)
{
result = result + items[i];
}
else
{
result = items[i] + result;
}
}
Run Code Online (Sandbox Code Playgroud) 假设我有 4 个变量
bool value1 = false;
bool value2 = false;
bool value3 = true;
bool value4 = false;
Run Code Online (Sandbox Code Playgroud)
并且它们都没有相同的值(都是真的,||都是假的)
我找到了 2 种方法,无论如何它们看起来都不容易理解。
bool areDifferent = !(value1 == value2 == value3 == value4);
Run Code Online (Sandbox Code Playgroud)
和
bool areDifferent = new[] { value1, value2, value3, value4 }.Distinct().Count() > 1;
Run Code Online (Sandbox Code Playgroud)
问题:是否有其他方法,在可读性/可理解性方面更好?
我有一个 string
string cubeinline = "12345123451234X1234512345";
Run Code Online (Sandbox Code Playgroud)
等于a List<string>
List<string> cube = new List<string>(){ "12345",
"12345",
"1234X",
"12345",
"12345"};
Run Code Online (Sandbox Code Playgroud)
但不同的安排.字符串按长度分割.在这种情况下5.
现在我需要将字符串与List进行比较 - char by char.但是我的方法说每个字符都是无效的.
int maxLength = 5;
for (int i = 0; i < cubeinline.Length; i++)
{
if (cubeinline[i] == cube[i / maxLength][i % maxLength])
{
Console.WriteLine("Error in char" + i);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个关于这个专栏的表
ALTER TABLE TestTable ADD TestColumn AS '1'
Run Code Online (Sandbox Code Playgroud)
如何将其更改为2?
ALTER TABLE TestTable ALTER COLUMN TestColumn AS '2'
Run Code Online (Sandbox Code Playgroud)
不起作用.
关键字"AS"附近的语法不正确.
删除和添加列不是一个选项.
我有一个输入值,例如
decimal input = 100.4m;
Run Code Online (Sandbox Code Playgroud)
和一个比较值/一个可以改变的容差。所以在这个例子中99.5to100.5是有效的,其他输入值不是。
decimal tolerance = 0.5m;
decimal compareValue = 100m;
Run Code Online (Sandbox Code Playgroud)
问题:有没有比这更优雅的验证方式:
bool isValid = (input >= compareValue - tolerance) && (input <= compareValue + tolerance);
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
bool value = false;
if(value)
{
Console.Write("true");
}
else
{
Console.Write("false");
}
Run Code Online (Sandbox Code Playgroud)
我想通过使用条件运算符缩短它,但我找不到正确的语法.
bool value = false;
value ? Console.Write("true") : Console.Write("false"); // does not work
Run Code Online (Sandbox Code Playgroud) 我有3个可能的输入案例
string input = ""; // expected result: ""
string input = "bar-foo"; // expected result: "foo"
string input = "foo"; // expected result: "foo"
Run Code Online (Sandbox Code Playgroud)
我必须删除包括第一个分隔符char的所有内容(- 如果存在).
工作方式:
string output = input.Split('-').LastOrDefault();
Run Code Online (Sandbox Code Playgroud)
我想解决这个问题Split()- 我的工作方法是:
string output = input.Substring(input.IndexOf('-') );
Run Code Online (Sandbox Code Playgroud)
如何处理IndexOutOfRangeException/使此代码工作?
我有这个switch语句
string status = "1";
switch (status)
{
case "1":
Button1.Visible = true;
Button2.Visible = true;
Button3.Visible = true;
Button4.Visible = true;
Button5.Visible = true;
Panel1.Visible = true;
break;
case "2":
Button1.Visible = true;
Button2.Visible = true;
Button3.Visible = true;
Button4.Visible = true;
Button5.Visible = true;
Panel2.Visible = true;
break;
}
Run Code Online (Sandbox Code Playgroud)
并且存在一些代码冗余.在这两种情况下Button1- 5显示和Panel引用status值.
有没有办法缩短代码?
我的方法是冗余度更低但线路更多 - 还有另一种我没有想到的明显方法吗?
string status = "1";
switch (status)
{
case "1":
case "2":
Button1.Visible = true;
Button2.Visible = …Run Code Online (Sandbox Code Playgroud) 我有一个简单的类,具有一个 ID 和多个其他属性
public class Item
{
public int ID { get; set; }
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我有List<Item>很多这些物品
List<Item> items = new List<Item>()
{
new Item() { ID = 1, Prop1="foo" , Prop2= "foo" },
new Item() { ID = 2, Prop1="foo" , Prop2= "foo" },
new Item() { ID = 3, Prop1="foo" , Prop2= "foo" }
};
Run Code Online (Sandbox Code Playgroud)
问题:当我想Item通过 ID 替换 a 的新版本时,是否有比这更好/更聪明的方法:
//new …Run Code Online (Sandbox Code Playgroud) 我有这个代码:
Dictionary<string, string> items = new Dictionary<string, string>();
if(TextBox1.Text != "")
{
items.Add(TextBox1.Name, TextBox1.Text);
}
if (TextBox2.Text != "")
{
items.Add(TextBox2.Name, TextBox2.Text);
}
if (TextBox3.Text != "")
{
items.Add(TextBox3.Name, TextBox3.Text);
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但有大约20 TextBox项,我想减少减少.有没有办法缩短它?
我已经看到使用此where关键字的泛型方法
public static void DoStuff<T>() where T :
Run Code Online (Sandbox Code Playgroud)
where似乎以T特定的方式限制了类型.
问题:这是如何where工作的?它是否有可能像接口这样where T : IComparable<T>或者有不同的方式?
我有多行输入
<input type="text" style="width: 50px;">
<br>
<input type="text" style="width: 25px;">
<input type="text" style="width: 25px;">Run Code Online (Sandbox Code Playgroud)
不幸的是,25+25 != 50因为它们之间存在空间,每个浏览器都有所不同.
问题:如何设置输入的样式2x25具有相同的长度1x50