Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.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# 是否可以将数组与OrderBy方法一起使用?_C#_Arrays_Sorting - Fatal编程技术网

C# 是否可以将数组与OrderBy方法一起使用?

C# 是否可以将数组与OrderBy方法一起使用?,c#,arrays,sorting,C#,Arrays,Sorting,我想从SQL查询中按表中的变量“StorageLocation”排序,并按定义的数组对其排序 我的数组:string[]sortedLocations={“位置A”、“位置B”、“位置2”} 我尝试的内容:reportRecords.OrderBy(o=>o.StorageLocation==sortedLocations[o]) 此代码当前具有所需的结果。它将按我首先设置的存储位置排序,然后将其余的附加到末尾。当我更改/重新排序StorageLocation时,这有点乏味 public voi

我想从SQL查询中按表中的变量“StorageLocation”排序,并按定义的数组对其排序

我的数组:
string[]sortedLocations={“位置A”、“位置B”、“位置2”}

我尝试的内容:
reportRecords.OrderBy(o=>o.StorageLocation==sortedLocations[o])

此代码当前具有所需的结果。它将按我首先设置的存储位置排序,然后将其余的附加到末尾。当我更改/重新排序StorageLocation时,这有点乏味

public void ActiveReport\u ReportStart()
{
//创建报表查询并设置报表数据源
使用(var context=sqlDB.CreateContext())
{
var reportRecords=(来自context.IssuedItems中的ii)
式中(ii.Item.BarcodeId.Length==9)
选择新的
{
ii.项目存储位置,
ii.Item.BarcodeId,
ii.User.UsernameId
}).ToList();
reportRecords=reportRecords.OrderBy(o=>
//先按定义的存储位置订购
o、 StorageLocation==“位置A”?1:
o、 StorageLocation==“位置B”?2:
o、 StorageLocation==“位置2”?3:4
).ThenBy(o=>o.BarcodeId).ToList();
rpt.DataSource=报告记录;
}
}
非常感谢您的帮助!:)

这样会更有效吗

List definedLocations=新列表()
{
“地点A”,
“地点B”,
“位置2”
};
列表排序=(从定义位置的e开始)
orderby e.BarcodeId
选择e.ToList();

您可以将IComparer的实现作为第二个参数传递给OrderBy。然后在该比较器中,可以添加逻辑以使用数组作为比较

注意:我相信你可以让我的“左/右”更有效率,但它确实做到了。我这样做是为了让未找到的元素放在最后。在知道列表中的元素之后,您还可以对列表中的元素进行常规字符串比较

static void Main(string[] args)
{
    var mylist = new List<string> { "Location 2", "Location A", "Location B", "Location X" };
    mylist = mylist.OrderBy(t => t, new MyComparer()).ToList();
    Console.WriteLine(string.Join(", ", mylist));
}

class MyComparer : IComparer<string>
{
    List<string> MyOrder = new List<string> { "Location A", "Location 2", "Location B" };

    public int Compare(string x, string y)
    {
        var left = MyOrder.IndexOf(x) == -1 ? int.MaxValue : MyOrder.IndexOf(x);
        var right = MyOrder.IndexOf(y) == -1 ? int.MaxValue : MyOrder.IndexOf(y);
        return left - right;
    }
}
static void Main(字符串[]args)
{
var mylist=新列表{“位置2”、“位置A”、“位置B”、“位置X”};
mylist=mylist.OrderBy(t=>t,newmycomparer()).ToList();
Console.WriteLine(string.Join(“,”,mylist));
}
类别MyComparer:I Comparer
{
List MyOrder=新列表{“位置A”、“位置2”、“位置B”};
公共整数比较(字符串x、字符串y)
{
var left=MyOrder.IndexOf(x)=-1?int.MaxValue:MyOrder.IndexOf(x);
var right=MyOrder.IndexOf(y)=-1?int.MaxValue:MyOrder.IndexOf(y);
返回左-右;
}
}

我找到了问题的解决方案:

// create array for locations to order reportRecords by
String[] definedLocations = {
    "Location A", 
    "Location B", 
    "Location 2"
};

// create a table then add the array definedLocations
var sortedLocations = new List<String>();
sortedLocations.AddRange(definedLocations);
在我的情况下,唯一的不良影响是未在
definedLocations
中列出的任何位置都被置于列表的顶部。因此,如果
StorageLocation
有“Location Y”,它将位于“Location A”之前


那将是另一天的战斗。谢谢你的评论。

你可以把它做成一本
字典{[1]=“Location a”,“[2]=…}
,然后它就是
OrderBy(o=>sortedLocations.GetValueOrDefault(o.StorageLocation,4))
@juharr谢谢你的回复。我试图避开数组中的索引号,只是根据第一个元素对其进行排序。我在一个SQL数据库中有很多位置,我可能需要修改数组,以便在“位置a”之前显示一个不同的位置,因此我想轻松地将其添加到顶部。我编辑了这篇文章,希望能有一个更清晰的例子。
// IndexOf is used to match strings in sortedLocations with StorageLocations
reportRecords = reportRecords.OrderBy(o => sortedLocations.IndexOf(o.StorageLocation)).ToList();