Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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# 订购我的LINQ_C#_Linq - Fatal编程技术网

C# 订购我的LINQ

C# 订购我的LINQ,c#,linq,C#,Linq,我有一个加入分类的公司列表,我通过自由文本输入进行搜索,通常按名称/字母顺序排列结果 但是,当返回的公司链接到某个类别时,这些更重要类别的id在一个列表中,我想主要排序结果,这些类别中的公司位于顶部,次要排序应按字母顺序排列 如何添加此订单 List<int> importantCategories = new List<int>() { 14, 99, 4428 }; var companies = (from c in context.Compani

我有一个加入分类的公司列表,我通过自由文本输入进行搜索,通常按名称/字母顺序排列结果

但是,当返回的公司链接到某个类别时,这些更重要类别的id在一个列表中,我想主要排序结果,这些类别中的公司位于顶部,次要排序应按字母顺序排列

如何添加此订单

    List<int> importantCategories = new List<int>() { 14, 99, 4428 };

    var companies = (from c in context.Companies
                     join ct in context.Categories on c.CategoryId equals ct.CategoryId
                     where ct.Active == true 
                     && c.Name.Contains(freeTextFilter)
                     orderby c.Name
                       --- if company is from category 14, 99, 4428 
                           then the ordering should place those above the rest
                     select c);
我从查询中删除了几行,但在我为什么不使用联接类别表的问题之后添加了其中一行

我会试试看

orderby (importantCategories.Contains(ct.CatetoryId) 
                            ? 0
                            : 1),
         c.Name
也许是这样

List<int> importantCategories = new List<int>() { 14, 99, 4428 };

var companies = (from c in context.Companies
                 join ct in context.Categories on c.CategoryId equals ct.CategoryId
                 where c.Name.Contains(freeTextFilter)
                 orderby importCategories.Contains(ct.CategoryID) ? 0 : 1, c.Name
                 select c);
4429只是可以动态计算的列表+1的最大值

正如所建议的那样,这也会起作用,但如果您想按类别ID订购,则不会起作用:

orderby !importCategories.Contains(ct.CategoryID) , c.Name

没有使用ct有什么原因吗?

首先,我认为您不需要进行连接,因为您实际上没有使用类别中的任何数据。您仅使用类别id进行筛选,而公司已包含该数据。其次,您应该将OrderBy与自定义键选择器和ThenBy一起使用,以便在类别中按字母顺序排列。这应该使用流畅的语法来完成

它首先按类别id排序,但仅当它们位于指定的类别中时,其他条目才被视为相同的int.MaxValue。然后在类别选择中按名称排序。如果您不关心第一节中类别的顺序,可以使用c=>categories.Containsc.CategoryId?0:1作为键选择器

var categories = new int[] { 14, 99, 4428 };

var companies = context.Companies
                       .Where(c => c.Name.Contains(freeTextFilter))
                       .OrderBy(c = categories.Contains(c.CategoryId) 
                                        ? c.CategoryId 
                                        : int.MaxValue)
                       .ThenBy(c => c.Name);

您可以使用ThenBy:下面是一个示例

 List<string> strings = new List<string> { "f", "a", "b", "c", "d", "e" };

        var result = strings.OrderByDescending(x => x == "e")
                            .ThenByDescending(x => x == "c")
                            .ThenBy(x=>x);
这就得到了e,c,a,b,d,f


我认为您可以将其应用于您的问题,以获得正确的顺序。

这与Hogan的答案类似,只使用扩展方法和Lambda表达式,而不是查询语法,尽管可以说这可能是一种查询语法更容易理解的情况

var companies = context.Companies
    .Join(context.Categories, c => c.CategoryId, ct => ct.CategoryId, (c, ct) => c)
    .Where(c => c.Name.Contains(freeTextFilter))
    .OrderBy(c => importantCategories.Contains(c.CategoryId) ? 0 : 1)
    .ThenBy(c => c.Name);
假设你真的不需要加入

var companies = context.Companies
    .Where(c => c.Name.Contains(freeTextFilter))
    .OrderBy(c => importantCategories.Contains(c.CategoryId) ? 0 : 1)
    .ThenBy(c => c.Name);

你需要这个吗?按布尔点餐行吗?@svick-我喜欢。。。进行我没有做的测试:这比在orderby语句中使用contains要慢得多-两个额外的循环。。。基本上,我的结局和你的非常相似。@Hogan-在什么情况下这“不起作用”?尝试执行代码-在我的机器上工作!这是一个按特定事物排序的示例,然后让其余的排序继续正常进行,这是OP的问题。这不是一个完整的重新工作的OP的问题与重新编写的代码。正如建议的答案中所述,这可以应用于问题,以提供一个稍加思考的解决方案。对,除了问题不是看一个值是否等于另一个值,而你的答案做得很好,也很好地显示了这一点外,问题是看一个值是否包含在另一个列表中。这并没有解决问题的contains方面,因为实际上只有一个解决方案,请使用contains函数。OrderBy表达式没有理由不能执行contains函数。在我的示例中,y=newlist{e,f}var result=string.OrderByx=>y.Containsx.ThenByx=>x。事实上,如果你看看下面tvanfosson提出的答案,他正是这么想的;我在注释中输入的代码应该是vary=newlist{e,c};var result=strings.orderbydegendingx=>y.Containsx.ThenByx=>x;对不起,我分心了!确切地说,您需要包含。
var companies = context.Companies
    .Where(c => c.Name.Contains(freeTextFilter))
    .OrderBy(c => importantCategories.Contains(c.CategoryId) ? 0 : 1)
    .ThenBy(c => c.Name);