English 中文(简体)
LINQ 命令 而不是命令...... 改变什么.. 为什么?
原标题:LINQ OrderBy not ordering .. changing nothing .. why?

任何知道为什么 LINQ 命令By 没有在以下代码中工作的原因, (没有错误, 但方法并不排序... )

首先是我自己的类型

public class IQLinksView
    {
        public int id { get; set; }
        public int catid { get; set; }
        public int? viewed {get;set;}
        public string name {get;set;}
        public string desc {get;set;}
        public string url {get;set;}
        public string pic {get;set;}
        public string cat {get;set;}
    }

然后查询 :

IQueryable<IQLinksView> newView = 
              from links in this.emContext.tbl_otherlinks
              select new IQLinksView { id = links.pklinkid, catid =
              links.tbl_catgeory.pkcategoryid, viewed = links.linkviewed, name = links.linkname, 
              desc = links.linkdesc, pic = links.linkpicture,   url = links.linkurl, cat =
              links.tbl_catgeory.categoryname };

直到这里所有的罚款: -,但然后

newView.OrderBy(x => x.viewed);

页面显示的是装货结果 但没有命令 嗅闻

我尝试过( 创建比较器对象... ) :

newView.OrderBy(x => (Int32)x.viewed, new CompareIntegers());

相同结果,无命令...

我确实有变通办法,但只是想知道缺少什么....

任何建议都会感激不尽:-)

最佳回答

不要丢弃返回值。 OrderBy 扩展法是 not 转换输入。 尝试 :

newView = newView.OrderBy(x => x.viewed);

如果 viewed 值是正确的,没有理由不起作用。此外,确保 OrderBy 是在任何会破坏订单的操作(例如 Distinct )之后。

快乐的编码!

问题回答

< a href=" "https://learn.microsoft.com/en-us/ef/ef/core/quering/ tracking#no- tracking-queries" rel= "不跟随 norefererr" > No-Tracking Queries

如果结果为只读结果, 则考虑在顺序() 之后使用自动跟踪( ) 。

示例:

query = query.OrderByDescending(x => x.Rating).AsNoTracking();




相关问题
IQueryable into a hierarchy

I currently have an IQueryable of Questions. In my Question object I have and "id" and a "parentId" which can be used to create a hierarchy. Currently, I bind a RadTreeView to the IQueryable of ...

sorting of an Iqueryable

I have an IQueryable with duplicate entries and I want to sort this IQueryable by the count of occurrences.

Determine the position of an element in an IQueryable

I have a IQueryable which is ordered by some condition. Now I want to know the position of a particular element in that IQueryable. Is there a linq expression to get that. Say for example there are ...

Linq-Sql IQueryable<T> and chaining OR operations

I m trying to simulate: WHERE x.IsActive = true OR x.Id = 5 The following causes AND to be used... how do I simulate an OR condition with IQueryable (qry) and my nullable int, given that other ...

Linq, Where Extension Method, Lambda Expressions, and Bool s

Greetings, I am having some issues with using a bool operation within a Where clause extension method of an IQueryable object obtained using Linq to Entities. The first example is showing what does ...

Linq - How to turn IQueryable<IEnumerable> into IQueryable

I have a simple linq statement that isn t quite returning what I would like. I understand why, I just don t know how to wriet it to get what I want. The query is as follows: answers = from a in ents....

热门标签