Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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# 按字母顺序排列列表,但某些元素始终位于顶部_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 按字母顺序排列列表,但某些元素始终位于顶部

C# 按字母顺序排列列表,但某些元素始终位于顶部,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我想使用实体框架Lambda表达式执行此查询。 我试着用OrderBy和ThenBy来实现这一点,但还没有找到合适的解决方案。我认为您应该添加新的Order属性,然后设置您最喜欢的如果您想保持它的灵活性,您应该扩展您的类并添加另一个属性来指示排序/显示顺序。那么你能在这上面分类或过滤吗 class Employee { static void Main(string[] args) { List<favorite> f = new List<

我想使用实体框架Lambda表达式执行此查询。

我试着用
OrderBy
ThenBy
来实现这一点,但还没有找到合适的解决方案。

我认为您应该添加新的
Order
属性,然后设置您最喜欢的如果您想保持它的灵活性,您应该扩展您的类并添加另一个属性来指示排序/显示顺序。那么你能在这上面分类或过滤吗

class Employee   
{
    static void Main(string[] args)
    {
        List<favorite> f = new List<favorite>();

        f.Add(new favorite {id = 1, title ="MEN" });
        f.Add(new favorite { id = 2, title = "WOMEN" });
        f.Add(new favorite { id = 3, title = "BOYS" });
        f.Add(new favorite { id = 4, title = "GIRLS" });
        f.Add(new favorite { id = 5, title = "Ajay" });
        f.Add(new favorite { id = 6, title = "vijay" });
        f.Add(new favorite { id = 7, title = "Jitu" });
        f.Add(new favorite { id = 8, title = "Suresh" });
        f.Add(new favorite { id = 9, title = "Ramesh" });
        f.Add(new favorite { id = 10, title = "Akshay" })

        foreach (var item in f.OrderBy(e=>e.title).ThenBy(e=>e.title))
        {
            Console.WriteLine(item.title);
        }
        Console.ReadLine();
    }

    class favorite
    {
        public int id { get; set; }
        public string title { get; set; }
    }
}

或者,您可以硬编码默认类的ID,基本上创建两个列表,在每个列表排序后合并在一起,但这不是很灵活。

最好的方法是添加一个额外的字段

如果您想使用“仅链接”解决此问题,您可以通过检查大写字母来排序。我想最上面的都是大写的,这样你就可以订购了

class favorite
{
    public favorite(){sortOrder = int.MaxValue;} // default constructor, set the sort order to highest value possible. Alternatively you can make this a nullable property with default null value.
    public int id { get; set; }
    public int sortOrder { get; set; } // default high, sorted on low to high
    public string title { get; set; }
}

// code in main method
f.Add( new favorite {id = 1, title ="MEN", sortOrder=1 });
f.Add(new favorite { id = 2, title = "WOMEN", sortOrder=2 });
f.Add(new favorite { id = 3, title = "BOYS", sortOrder=3 });
f.Add(new favorite { id = 4, title = "GIRLS", sortOrder=4 });
// rest of your code, note that boolean defaults to false if you do not set it so the rest of your instances do not require change

// Sort by the sort order first and then title. Sort order is small first to large last, if they are the same then they are sorted by title
foreach (var item in f.OrderBy(e=>e.sortOrder).ThenBy(e=>e.title))
{
   Console.WriteLine(item.title);
}
static void Main(字符串[]args)
{
列表f=新列表();
f、 添加(新收藏夹{id=1,title=“MEN”});
f、 添加(新收藏夹{id=2,title=“WOMEN”});
f、 添加(新收藏夹{id=3,title=“BOYS”});
f、 添加(新收藏夹{id=4,title=“GIRLS”});
f、 添加(新收藏夹{id=5,title=“Ajay”});
f、 添加(新收藏夹{id=6,title=“vijay”});
f、 添加(新收藏夹{id=7,title=“Jitu”});
f、 添加(新收藏夹{id=8,title=“Suresh”});
f、 添加(新收藏夹{id=9,title=“Ramesh”});
f、 添加(新收藏夹{id=10,title=“Akshay”});
var list=f.OrderByDescending(e=>IsAllUpper(e.title)).ThenBy(e=>e.title.ToList();
foreach(列表中的变量项)
{
控制台写入线(项目名称);
}
Console.ReadLine();
}
班级最爱
{
公共int id{get;set;}
公共字符串标题{get;set;}
}
静态布尔值IsAllUpper(字符串输入)
{
for(int i=0;i
如果您不想更改
收藏夹
类,您可以在某处定义这样的函数

     static void Main(string[] args)
    {
        List<favorite> f = new List<favorite>();

        f.Add(new favorite { id = 1, title = "MEN" });
        f.Add(new favorite { id = 2, title = "WOMEN" });
        f.Add(new favorite { id = 3, title = "BOYS" });
        f.Add(new favorite { id = 4, title = "GIRLS" });
        f.Add(new favorite { id = 5, title = "Ajay" });
        f.Add(new favorite { id = 6, title = "vijay" });
        f.Add(new favorite { id = 7, title = "Jitu" });
        f.Add(new favorite { id = 8, title = "Suresh" });
        f.Add(new favorite { id = 9, title = "Ramesh" });
        f.Add(new favorite { id = 10, title = "Akshay" });

        var list = f.OrderByDescending(e => IsAllUpper(e.title)).ThenBy(e => e.title).ToList();
        foreach (var item in list)
        {
            Console.WriteLine(item.title);
        }
        Console.ReadLine();
    }


    class favorite
    {
        public int id { get; set; }
        public string title { get; set; }
    }

    static bool IsAllUpper(string input)
    {
        for (int i = 0; i < input.Length; i++)
        {
            if (Char.IsLetter(input[i]) && !Char.IsUpper(input[i]))
                return false;
        }
        return true;
    }
并将其用于初始订购,如

private static int GetPrimaryOrder(string title)
    {
        switch (title)
        {
            case "MEN":
                return 1;
            case "WOMEN":
                return 2;
            case "BOYS":
                return 3;
            case "GIRLS":
                return 4;
            default:
                return 5;
        }
    }

让我们看看你到目前为止都做了些什么。向我们展示您编写的不起作用的查询。我认为上述查询是错误的。我没有使用默认收藏夹做任何事情。有什么建议吗???@ajaysingh您可以为您的
收藏夹类添加其他字段吗?但所有收藏夹都是大写的。在这种情况下,我用小写而不是默认收藏夹定义了列表,这是我的错误我会在订单上添加一个额外的属性。
foreach (var item in f.OrderBy(x => GetPrimaryOrder(x.title)).ThenBy(e => e.title))
{
     Console.WriteLine(item.title);
}