Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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#_Asp.net_Entity Framework_Linq - Fatal编程技术网

C# 使用带关系的动态Linq排序

C# 使用带关系的动态Linq排序,c#,asp.net,entity-framework,linq,C#,Asp.net,Entity Framework,Linq,我试图通过相关对象的属性进行排序。举个例子,情况就是这样。我有两门课: public class Publication { public int Id { get; set; } public string Name { get; set; } public List<Product> Products { get; set; } } public class Product { public int Id { get; set; } pu

我试图通过相关对象的属性进行排序。举个例子,情况就是这样。我有两门课:

public class Publication
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Product> Products { get; set; }
}

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int PublicationId { get; set; }
    [ForeignKey("PublicationId")]
    public Publication Publication { get; set; }
    public int Category { get;set; }
}
公共类发布
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表产品{get;set;}
}
公共类产品
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public int PublicationId{get;set;}
[ForeignKey(“PublicationId”)]
公共发布发布{get;set;}
公共int类{get;set;}
}
现在,我需要获取产品类别为1并按出版物Id desc排序的所有出版物:

IQueryable<Publication> query = db.Publications.Where("Products.Any(Category = 1)");
List<Publication> list = query.OrderBy("Id desc").ToList();
IQueryable query=db.Publications.Where(“Products.Any(Category=1)”;
List List=query.OrderBy(“Id desc”).ToList();

这是伟大的工作!但是如何使用动态Linq按产品类别(desc或asc)订购出版物?

我还没有找到如何使用
System.Linq.Dynamic
订购出版物,但对于
Linq
这是可行的

var query = publications.OrderByDescending(p => p.Products.Select(x => x.Category).FirstOrDefault()).ToList();
以下是测试类:

var publications = new List<Publication>
        {
            new Publication
            {
                Products = new List<Product>
                {
                    new Product {Category = 5},
                    new Product {Category = 6},
                    new Product {Category = 7}
                }
            },
            new Publication
            {
                Products = new List<Product>
                {
                    new Product {Category = 2},
                    new Product {Category = 3},
                    new Product {Category = 4}
                }
            },
            new Publication
            {
                Products = new List<Product>
                {
                    new Product {Category = 8},
                    new Product {Category = 9},
                    new Product {Category = 10}
                }
            }
        };

        var query = publications.OrderByDescending(p => p.Products.Select(x => x.Category).FirstOrDefault()).ToList();

        query.ForEach(x => Console.WriteLine(x.ToString()));

正如您所看到的,它不会在内部订购
产品

难道您不能只进行OderByDescending吗?()订单(asc或desc)现在不是问题。问题是如何通过关系排序(在asc或desc中)。在这种情况下,类别(来自产品)
出版物
有一个或多个产品,因此是类别。您首先应该定义如何对类别集合进行排序。出版物有一个或多个产品。类别只是产品的一个整数。我只需要根据产品类别的数量对出版物进行排序。相关?谢谢,但我需要动态Linq中的这个。好吧,我来看看,你的订单标准是什么?但出于好奇,如果你可以使用“普通”的,为什么要使用动态Linq?这是因为我在webapi中从url构建字符串查询。这些都是动态生成的。一切正常。我只是需要这个来完成这个项目。你点的菜没问题。出版物根据嵌套产品类别按asc或desc排序。我这样做的方式是按找到的第一个产品的类别排序,如果没有,则为0,这是您的标准吗?我认为可以
8 , 9 , 10
5 , 6 , 7
2 , 3 , 4