简体   繁体   English

实体框架在数据库中不存在的列上排序

[英]Entity Framework sort on column that don't exist in the database

In my model I have a property that is a combination of other models. 在我的模型中,我有一个属性,它是其他模型的组合。

        public string PiramideId => (string.IsNullOrWhiteSpace(GEMEENTECODE) ? "" : GEMEENTECODE.Trim()) + "-" + (string.IsNullOrWhiteSpace(WIJKCODE) ? "" : WIJKCODE.Trim()) + "-" + (string.IsNullOrWhiteSpace(GROEPCODE) ? "" : GROEPCODE.Trim()) + "-" + (string.IsNullOrWhiteSpace(STRAATCODE) ? "" : STRAATCODE.Trim()) + "-" + (string.IsNullOrWhiteSpace(GEBOUWCODE) ? "" : GEBOUWCODE.Trim()) + "-" + (string.IsNullOrWhiteSpace(HUISNR) ? "" : HUISNR.Trim());

When I do a call in Entity Framework when I try to sort on PiramideId EF returns an error saying that the column does not exist in the database. 当我尝试对PiramideId进行排序时,在Entity Framework中进行调用时,EF返回一个错误,指出该列在数据库中不存在。 Is there a way to tell EF that the column PiramideId is basically a few sorts combined together? 有没有办法告诉EF PiramideId列基本上是几种组合在一起的?

I tried sorting after getting a list of all items, and even though that works it's not what I want, because then I have to get all records in my db while I only want the top 30. 我在获得所有项目的列表后尝试进行排序,尽管那行得通,但这并不是我想要的,因为那样我就必须在数据库中获取所有记录,而我只想要前30个。

Example of query: 查询示例:

PagedView.Context.PERSOON.Join(PagedView.Context.VERHURINGEN, persoon => persoon.ComputerNr,
                        verhuring => verhuring.PersoonsID, (persoon, verhuring) => new {persoon, verhuring})
                    .Join(PagedView.Context.EENHEID, @t => @t.verhuring.Eenheid, eenheid => eenheid.ComputerNr,
                        (@t, eenheid) => new PersoonDTO
                        {
                            ComputerNr = @t.persoon.ComputerNr,
                            FAMILIENAAM = @t.persoon.FAMILIENAAM,
                            VOORNAAM = @t.persoon.VOORNAAM,
                            NAAM = @t.persoon.NAAM,
                            ADRES = @t.persoon.ADRES,
                            Een = eenheid
                        }).OrderBy(p => p.Een.PiramideId).ToList();

Entity Framework works by generating a SQL statement, querying the database, then mapping the results on to the model. 实体框架通过生成SQL语句,查询数据库,然后将结果映射到模型来工作。 In this particular case it can't produce the SQL statement as it has no idea what PiramideId is. 在这种情况下,由于不知道PiramideId是什么,因此无法生成SQL语句。 You can get round this by performing the query, then sorting. 您可以通过执行查询然后进行排序来解决此问题。 The easiest way to get it to perform the query is using AsEnumerable . 使它执行查询的最简单方法是使用AsEnumerable This gives you 这给你

PagedView.Context.PERSOON
                 .Join(PagedView.Context.VERHURINGEN, 
                       persoon => persoon.ComputerNr,
                       verhuring => verhuring.PersoonsID,
                       (persoon, verhuring) => new {persoon, verhuring})
                 .Join(PagedView.Context.EENHEID,
                       @t => @t.verhuring.Eenheid,
                       eenheid => eenheid.ComputerNr,
                       (@t, eenheid) => new PersoonDTO
                       {
                           ComputerNr = @t.persoon.ComputerNr,
                           FAMILIENAAM = @t.persoon.FAMILIENAAM,
                           VOORNAAM = @t.persoon.VOORNAAM,
                           NAAM = @t.persoon.NAAM,
                           ADRES = @t.persoon.ADRES,
                           Een = eenheid
                       })
                 .AsEnumerable()
                 .OrderBy(p => p.Een.PiramideId)
                 .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM