Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将逗号分隔的字符串分组到范围组2维数组中_C#_Asp.net_Arrays_Sql Server_Multidimensional Array - Fatal编程技术网

C# 将逗号分隔的字符串分组到范围组2维数组中

C# 将逗号分隔的字符串分组到范围组2维数组中,c#,asp.net,arrays,sql-server,multidimensional-array,C#,Asp.net,Arrays,Sql Server,Multidimensional Array,我有一个逗号分隔的字符串,需要将其分解为数组范围,这些数组范围可以传递到SQLServer的BEVERY语句中 例如,假设我有下面的字符串,我需要返回相应的returnVal string delimited = "1,2,5,6,7,8,11,12,13,15,16,17,18,19"; returnVal = int[,] ranges = new int[4, 2] { { 1, 2 }, { 5, 8 }, { 11, 13 }, { 15, 19 } }; 我的SQLBetween

我有一个逗号分隔的字符串,需要将其分解为数组范围,这些数组范围可以传递到SQLServer的BEVERY语句中

例如,假设我有下面的字符串,我需要返回相应的returnVal

string delimited = "1,2,5,6,7,8,11,12,13,15,16,17,18,19"; 
returnVal = int[,] ranges = new int[4, 2] { { 1, 2 }, { 5, 8 }, { 11, 13 }, { 15, 19 } };
我的SQLBetween语句将如下所示

WHERE (ID BETWEEN 1 AND 2) OR (ID BETWEEN 5 AND 8)  OR (ID BETWEEN 11 AND 13)  OR (ID BETWEEN 15 AND 19) 
而不是

其中ID位于(1,2,5,6,7,8,11,12,13,15,16,17,18,19)

我的实际分隔字符串超过5000个ID,因此为了提高效率,我需要在语句之间使用分隔字符串,而不是在语句中使用分隔字符串


使用C#最有效的方法是什么?

以下内容将解决您的问题:

如果整数是分隔字符串,可以使用split将其转换为整数数组

var intArray = **array of integers**
var ranges = new List<List<int>>();

int? topRange = intArray[0];
int? lastId = null;
int bottomRange; 
foreach(int id in intArray)
{
    if(topRange == null)
    {
        topRange = id;
    }
    if (lastId != null && id != (lastId + 1))
    {
        bottomRange = lastId.Value;
        ranges.Add(new List<int>() { topRange.Value, bottomRange });
        topRange = id;
    }
    lastId = id;
}
var intArray=**整数数组**
变量范围=新列表();
智力?topRange=intArray[0];
智力?lastId=null;
int底部范围;
foreach(intArray中的int-id)
{
如果(topRange==null)
{
topRange=id;
}
如果(lastId!=null&&id!=(lastId+1))
{
bottomRange=lastId.Value;
添加(新列表(){topRange.Value,bottomRange});
topRange=id;
}
lastId=id;
}

以下内容将解决您的问题:

如果整数是分隔字符串,可以使用split将其转换为整数数组

var intArray = **array of integers**
var ranges = new List<List<int>>();

int? topRange = intArray[0];
int? lastId = null;
int bottomRange; 
foreach(int id in intArray)
{
    if(topRange == null)
    {
        topRange = id;
    }
    if (lastId != null && id != (lastId + 1))
    {
        bottomRange = lastId.Value;
        ranges.Add(new List<int>() { topRange.Value, bottomRange });
        topRange = id;
    }
    lastId = id;
}
var intArray=**整数数组**
变量范围=新列表();
智力?topRange=intArray[0];
智力?lastId=null;
int底部范围;
foreach(intArray中的int-id)
{
如果(topRange==null)
{
topRange=id;
}
如果(lastId!=null&&id!=(lastId+1))
{
bottomRange=lastId.Value;
添加(新列表(){topRange.Value,bottomRange});
topRange=id;
}
lastId=id;
}

范围的逻辑是什么?有14个数字,但您只使用了8。我看到了问题,没有看到任何提示后面逻辑的内容。如果您有5000个id,则可能问错了问题。创建一个带有索引的时态表并进行连接。给定的id值不能是“介于1和2之间”和“介于5和8之间”。因此,您的版本在逻辑上是不正确的。如果希望使用范围,则需要使用或,而不是和。同意Juan-Erland讨论TVP的使用好的观点,我已经根据你的评论修改了我的问题。范围的逻辑是什么?有14个数字,但您只使用了8。我看到了问题,没有看到任何提示后面逻辑的内容。如果您有5000个id,则可能问错了问题。创建一个带有索引的时态表并进行连接。给定的id值不能是“介于1和2之间”和“介于5和8之间”。因此,您的版本在逻辑上是不正确的。如果希望使用范围,则需要使用或,而不是和。同意胡安-厄兰讨论TVP的使用很好,我已经根据你的评论修改了我的问题。