小编Tos*_*shi的帖子

通用DataRow扩展

我使用扩展方法检查DataRowField是否为null

public static string GetValue(this System.Data.DataRow Row, string Column)
{
    if (Row[Column] == DBNull.Value)
    {
        return null;
    }
    else
    {
        return Row[Column].ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想知道我是否可以使这更通用.在我的情况下,返回类型始终是字符串,但列也可以是Int32或DateTime

就像是

public static T GetValue<T>(this System.Data.DataRow Row, string Column, type Type)
Run Code Online (Sandbox Code Playgroud)

c# datarow

3
推荐指数
1
解决办法
1257
查看次数

SQL 不区分大小写的 IN 搜索

我有这个"Table"包含内容的表格:

+--------+
| Serial |
+--------+
| d100m  | <- expected result
| D100M  | <- expected result
| d200m  | <- expected result
| d300L  |
| D400R  |
+--------+
Run Code Online (Sandbox Code Playgroud)

存储了大小写不准确的序列号。

目前我正在选择那里,声明如下

SELECT Serial FROM Table WHERE Serial LIKE 'D100M' OR Serial LIKE 'D200M';
Run Code Online (Sandbox Code Playgroud)

但是没有更简单的方法,而不是OR Serial LIKE OR Serial LIKE OR Serial LIKE我必须比较大约 30 个数字。

像这样的东西

SELECT Serial FROM Table WHERE Serial LIKE IN ('D100M', 'D200M')
Run Code Online (Sandbox Code Playgroud)

sql sql-like

3
推荐指数
1
解决办法
5983
查看次数

识别IEnumerable是否已排序的方法

我有这个Extension方法来检查是否对任何类型的List进行了排序

public static bool IsSorted<T>(this IEnumerable<T> input)
{
    IEnumerable<T> expectedListASC = input.OrderBy(x => x);
    IEnumerable<T> expectedListDESC = input.OrderByDescending(x => x);
    return expectedListASC.SequenceEqual(input) || expectedListDESC.SequenceEqual(input);
}
Run Code Online (Sandbox Code Playgroud)

但是对于大型列表,需要时间.是否有更有效的方法来获得相同的结果?

c# sorting list

3
推荐指数
1
解决办法
95
查看次数

实例化列表

如果我想实例化一个数组,语法是

int[] items = new int[] { 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)

快捷方式是

int[] items = {1,2,3};
Run Code Online (Sandbox Code Playgroud)

现在我想对 a 做同样的事情List

问题:

为什么这有效:

List<int> items = new int[] { 1, 2, 3 }.ToList();
Run Code Online (Sandbox Code Playgroud)

但不是这个:

List<int> items = { 1, 2, 3 }; //invalid
Run Code Online (Sandbox Code Playgroud)

或者

List<int> items = { 1, 2, 3 }.ToList(); //invalid
Run Code Online (Sandbox Code Playgroud)

c# arrays list instance

2
推荐指数
1
解决办法
3699
查看次数

在int.TryParse之前的Nullcheck

我有代码,许多字符串被解析为整数值.

string item = null; //or a value
int result;
if (!string.IsNullOrEmpty(item) && int.TryParse(item, out result))
{
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

是否真的需要IsNullOrEmpty每次检查?如果是null或为空,则解析应该失败.

c# int

0
推荐指数
1
解决办法
1314
查看次数

字符串加入列表和项目

我需要加入字符串和List<string>项目.我的守则

List<string> list = new List<string>() { "item1", "item2" };
string item3 = "item3";

string result = string.Join(",", list, item3); 
Run Code Online (Sandbox Code Playgroud)

结果是

//System.Collections.Generic.List`1[System.String],item3
Run Code Online (Sandbox Code Playgroud)

而不是预期的

//item1,item2,item3
Run Code Online (Sandbox Code Playgroud)

什么是最优雅的方法,以解决这个内联?可能有多个List<string>和多个string,每个都可以为null.

c# string

0
推荐指数
1
解决办法
557
查看次数

语法null condition operator jagged array

如何在锯齿状数组中使用空条件运算符的正确语法?

int[][][] array = new int[5][][];
// fill with data here
bool result = array[0]?.[3]?.[2] ?? == 3;
Run Code Online (Sandbox Code Playgroud)

该参考文献未提供任何有关该信息的信息

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators

c# null jagged-arrays

0
推荐指数
1
解决办法
59
查看次数

标签 统计

c# ×6

list ×2

arrays ×1

datarow ×1

instance ×1

int ×1

jagged-arrays ×1

null ×1

sorting ×1

sql ×1

sql-like ×1

string ×1