IEnumerable vs IQueryable

后端 未结 4 1673
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-23 15:30

I have a query:

topics.OrderBy(x => x.Replies.Any() ? x.Replies.OrderBy(y => y.PostedDate).Last().PostedDate : x.PostedDate);

It sorts th

4条回答
  •  有刺的猬
    2021-01-23 16:22

    I think EF is unable to translate your query correctly, it happens. Because it is not necessary that every lamda will convert successfully into esql, it has its own limitations. I think it is very complicated SQL query for what you are looking for.

    I will suggest you can create a new DateTime field in your topics called "LastUpdate", which will be defaulted to Topic's PostDate when it will be created. Second when you add a new reply, you make sure you update Topic's LastUpdate as well.

    By doing this, you also make your query very simple, and you avoid joining unnecessary while creating your query. You don't need to look for PostDate of replies table with any join.

    I do this kind of Caching (which is called denormalization in database design terms), this is not perfect in terms of design but by performance and coding, its much easier and simpler.

提交回复
热议问题