Linq包含位置...保持默认顺序 [英] Linq Where Contains ... Keep default order

查看:78
本文介绍了Linq包含位置...保持默认顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ID号集合,我希望为其返回一些对象,这是通过使用linq语句以及其中包含contains语句的地方来完成的:

I have a collection of ID numbers that I wish to return some object for, I'm doing it by using a linq statement with a where that's using a contains statement:

var recentCats = (from i in EntityCache.Default.GetAll<Categories>()
                          where WebProfile.Current.RecentlyCreatedCategories.Contains(i.Id)
                          && BoundCategory.ParentItemClass.Id.Equals(i.ParentItemClass.Id)
                          select new CategoryInfo()
                          {
                              Category = i,
                              ClassId = i.ParentItemClass.Id,
                              ClassImage = NamedResourceManager.GetResourceBinary(i.ParentItemClass.NameResourceId)
                          });

除了我想使返回的集合中的项目顺序与进入的列表中的项目顺序保持一致之外,其他方法都可以正常工作.例如,如果我有一个ID列表:14、603、388 ,我希望返回的对象具有相同的顺序,而不是由缓存返回的顺序.实体框架中有什么方法可以做到这一点,或者有什么方法不需要我编写foreach循环?

This works perfectly fine, except that I want to keep the order of items in the returned collection the same as they were in the list that goes in. So for example if I had a list of IDs: 14, 603, 388, I want the objects that come back to be in the same order, not the order that they're returned by the cache. Is there any way in entity framework to do this, or any way to do it that doesn't involve me writing a foreach loop?

谢谢

推荐答案

对于感兴趣的任何人,我都可以通过加入它们来使它们以与列表相同的顺序出现,正如Ray在上面的评论中提到的那样.

For anyone interested, I was able to get them to come out in the same order as the list by joining them, as mentioned by Ray in the comments above.

var recentCats = (from i in WebProfile.Current.RecentlyCreatedCategories
                              join b in allCats
                                on i equals b.Id
                              where BoundCategory.ParentItemClass.Id.Equals(b.ParentItemClass.Id)
                              select ...

再次感谢您的所有帮助&答案.

Thanks again for all your help & answers.

这篇关于Linq包含位置...保持默认顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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