实体框架和许多表上的订单

时间:2011-12-03 19:46:36

标签: c# entity-framework

var articles = (from a in context.Articles
                                    where a.Id != articleId
                                    orderby a.Categories ??Name?? ascending
                                    select a).ToList();

a.Categories是表格(与许多关系相关联)

public EntityCollection<Category> Categories
        {
            get
            {
                return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Category>("AsoModel.CategoryArticle", "Category");
            }
            set
            {
                if ((value != null))
                {
                    ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Category>("AsoModel.CategoryArticle", "Category", value);
                }
            }
        }

如何在“类别”表上使用“按名称升序”。

示例:

文章名称:足球和本文属于体育和新闻类别。因此,两个表由许多关系连接。现在我想按类别名称订购这篇文章,而新闻是第一,Sport是第二......

1 个答案:

答案 0 :(得分:2)

var articles = context.Articles.Where(a=>a.Id != articleId)
.OrderBy(p=>p.Categories.OrderBy(q=>q.Name).FirstOrDefault().Name).ToList();