English 中文(简体)
Nerd Dinner pagginated list——我如何动态地增加一条林克按条款? ANYONE?
原标题:Nerd Dinner pagginated list - how do I add a linq orderby clause dynamicaly? ANYONE?

我已经采用了精干的晚餐方法。 但是,我希望能够充满活力地通过一些田地实现秩序。

我将如何执行。 我的法典在这里?

主计长:

 PaginatedList<Classifieds_Ads> pageOfClassifieds = new PaginatedList<Classifieds_Ads>(classifiedsRepositry.GetClassifiedsInCategory(category), paging, 20);

交存:

    return from classifieds in context.Classifieds_Ads.Include("User")
           where (from catergory in context.Classifieds_Categories
                  where catergory.MVC_URL == MVC_Cat 
                  select catergory).Contains(classifieds.Classifieds_Categories)
            orderby classifieds.DatePosted descending
           select classifieds;

你可以看到,我有“硬编码”的顺序条款。 我看不出有活力地实施该守则。

没有人有什么想法?

感谢

最佳回答

您可以使用订单By(Of TSource, TKey)-Extensionmethod,并通过主要轨道参数通过定制功能。 也许这个小例子会给你一个如何开始的想法:

    class A
    {
        public String Foo { get; set; }
        public Int32 Bar { get; set; }
        public override string ToString()
        {
            return Foo + ":" + Bar.ToString();
        }
    }

    static void Main(string[] args)
    {
        var x = new List<A> { new A { Foo = "ABC", Bar = 100 }, new A() { Foo = "ZZZ", Bar = 0 } };
        Func<A, String> order1 = (a) => a.Foo;
        Func<A, Int32> order2 = (a) => a.Bar;

        PrintQuery(x, order1);
        Console.WriteLine();
        PrintQuery(x, order2);
        Console.ReadLine();
    }

    static void PrintQuery<T>(IEnumerable<A> query, Func<A, T> orderFunc)
    {
        foreach (var e in query.OrderBy(orderFunc))
            Console.WriteLine(e);
    }
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签