简体   繁体   English

Linq查询以获取最后一条记录

[英]Linq query to get the last record

I know this is asked many times and I've searched most of the solutions online, but nothing seems to make it for me. 我知道这个问题已经问了很多遍了,我已经在网上搜索了大多数解决方案,但是似乎没有什么适合我的。 I have a table with this structure: 我有一个具有这种结构的表:

ID | ScheduleId | Filename | Description
 1 |     10     |          |  ....
 2 |     10     | test.txt |  .....

I want to get the last non-empty Filename by passing the ScheduleId(eg to get "test.txt" in this case). 我想通过传递ScheduleId来获取最后一个非空Filename (例如,在这种情况下要获取“ test.txt”)。

I've tried many things and nothing seems to get me the Filename. 我已经尝试了很多事情,但似乎没有任何东西可以让我获得文件名。 Here is the last one: 这是最后一个:

var tempFileName = objContext.SchedulesAndFiles
                           .Where(x => x.ScheduleId == scheduleId)
                           .OrderByDescending(x => x.ScheduleId)
                           .Take(1).Select(x => x.Filename);

This doesn't work as well, although I understand why it doesn't: 尽管我理解为什么它不起作用,但这也不能正常工作:

var tempFileName = from e in objContext.SchedulesAndFiles
                   where e.ScheduleId == scheduleId 
                   orderby e.ScheduleId descending
                   select e.Filename;

Calling .Last() or .LastOrDefault() throws an exception( The query operator 'LastOrDefault' is not supported. ) 调用.Last().LastOrDefault()会引发异常( The query operator 'LastOrDefault' is not supported.

if have to include that you want only non-empty filenames. 如果必须包含,则只需要非空文件名。 You may also use ToList() to finalize the query, then FirstOrDefault() should work as expected, try 您也可以使用ToList()完成查询,然后FirstOrDefault()应该可以正常工作,请尝试

var tempFileName = objContext.SchedulesAndFiles
                             .Where(x 
                                 => x.ScheduleId == scheduleId 
                                 && x.Filename != null 
                                 && x.Filename != "")
                             .OrderByDescending(x => x.ScheduleId)
                             .Take(1)
                             .Select(x => x.Filename)
                             .ToList()
                             .FirstOrDefault();

You should sort your records based on the ID instead of ScheduleId and also filter the records that has the empty Filename : 您应该根据ID而不是ScheduleId对记录进行排序,并过滤具有空Filename的记录:

objContext.SchedulesAndFiles
          .Where(x => x.ScheduleId == scheduleId && x.Filename != "")
          .OrderByDescending(x => x.ID)
          .First().Filename;

You can try this query. 您可以尝试此查询。 I think you have to issue the last or default before selecting the file name 我认为您必须在选择文件名之前发出最后一个或默认值

 var tempFileName = objContext.SchedulesAndFiles
                               .Where(x => x.ScheduleId == scheduleId && ! string.IsNullOrEmpty(e.FileName))
                               .FirstOrDefault().Select(x => x.Filename);

One option is to call ToList() or AsEnumerable() before trying to use LastOrDefault(). 一种选择是在尝试使用LastOrDefault()之前调用ToList()或AsEnumerable()。

var tempFileName = objContext.SchedulesAndFiles
                   .Where(x => x.ScheduleId == scheduleId 
                            && x.Filename != null && x.Filename != '')
                   .ToList().LastOrDefault();
if(tempFileName != null)
{
    // Do something
}

The final attempt: 最后的尝试:

var tempFileName = objContext.SchedulesAndFiles
                         .Where(x 
                             => x.ScheduleId == scheduleId 
                             && x.Filename != null 
                             && x.Filename != "")
                         .OrderByDescending(x => x.ID)
                         .First()
                         .Select(x => x.Filename);

For every item with this scheduleId, this gets the all of the items which have a non-empty fileName, sorted descending by ID (assuming that higher IDs are inserted after lower IDs), getting the First() (should be supported) and getting its FileName. 对于具有此scheduleId的每个项目,这将获取所有具有非空fileName的所有项目,并按ID降序排列(假定在较低的ID之后插入较高的ID),获取First()(应受支持),并获取其FileName。

Note that you could run into a NullPointerException on First() if there is no satisfying fileName. 请注意,如果没有令人满意的fileName,则可能会在First()上遇到NullPointerException

Also, you may need to normalize/trim to not find spaces/tabs and such things. 另外,您可能需要规范化/修剪以找不到空格/制表符之类的东西。

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

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