如何获得IEnumerable清单的命令清单? [英] How to get IEnumerable list ordered alfabetical?

查看:90
本文介绍了如何获得IEnumerable清单的命令清单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

源代码:

Source code:

public IEnumerable<Customer> GetAllCustomers()
        {
            IEnumerable<Customer> tempCustomer;

            try
            {
                tempCustomer = (from p in DatabaseConnection.Customers select p);
                return tempCustomer;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }




我可以使用Order By函数在返回之前对列表进行排序吗?可以给个例子吗?还是有其他解决方案可以对它进行排序?




Can I use Order By function to get the list sorted before returning? And can any1 give an example? Or are there any other solution to do it sorted?

推荐答案

为什么不在LINQ查询中使用orderby LINQ子句.假设客户是具有属性名称的对象:

Why not use the orderby LINQ clause in your LINQ query. Assuming a Customer is an object with a property Name:

tempCustomer = (from p in DatabaseConnection.Customers orderby p.Name select p);



稍微偏离主题,但我还建议您尽可能将IEnumerable替换为适当的IEnumerable<T>.



Slightly off topic, but I also suggest you replace IEnumerable with an appropriate IEnumerable<T> if possible.


orderby正是我所需要的,只是错过了添加语法的位置.很棒...

IEnum已经在原始代码中进行类型转换,因此ot与ot相关.谢谢您的协助.
orderby was excatly what I needed, just missed where in the syntax to add it. Worked great...

The IEnum is already type casted in the original code, so the ot was relevant tho it was ot. Thnx for your assistance.


这篇关于如何获得IEnumerable清单的命令清单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆