简体   繁体   English

针对Azure表服务的LINQ查询失败

[英]LINQ Query against Azure table service fails

I have a few records saved in my Azure Table Storage. 我在Azure表存储中保存了一些记录。 I'm trying to pull records from a table,for only a particular day/date, for the currently logged in user. 我正在尝试从表中为当前登录的用户仅在特定的日期/日期中提取记录。

My query returns nothing, so my list is always empty, even though I know that I have 3 records for this month, in my table. 我的查询未返回任何内容,因此即使我知道我的表中本月有3条记录,我的列表也始终为空。

I'm not sure why the query fails in this case. 我不确定在这种情况下为什么查询失败。 How do I pull records for a specific date, for a specific user? 如何为特定用户提取特定日期的记录? Any help? 有什么帮助吗?

This is what I've tried so far: 到目前为止,这是我尝试过的:

 public async Task<Result<List<Alert>>> FetchAlertsForDate (DateTime date)
    {

  try {

       var fromDate = new DateTime(date.Year,date.Month,date.Day,0,0,0); //lets create a 12:00:00 AM date

       var toDate = new DateTime(date.Year,date.Month,date.Day,23,59,59); //lets create a 23:59:59PM date

       var alertTable=client.GetSyncTable<Alert>();

       var alerts = await alertTable.Where(n=>n.AccountId==client.CurrentUser.UserId).Where(n=> n.StartDate >= fromDate && n.StartDate <= toDate).ToListAsync();

       return Result<List<Alert>>.Success(alerts);


  } catch (Exception ex) {
            return Result<List<Alert>>.Failure (ex.Message + ex.StackTrace);    
        }

   }

The code you have written contains no obvious bugs to answer your question 您编写的代码没有明显的错误可以回答您的问题

How do I pull records for a specific date, for a specific user? 如何为特定用户提取特定日期的记录?

Rather than a date range you use in your method you can just compare the date part of a dateTime like this; 除了使用方法中的日期范围外,您还可以像这样比较dateTime的日期部分;

   var alerts = await alertTable.Where(n=>n.AccountId==client.CurrentUser.UserId).Where(n=> n.StartDate.Date == date.Date).ToListAsync();

I think you can do away with the second where clause too; 我认为您也可以取消第二个where子句;

   var alerts = await alertTable.Where(n=>n.AccountId==client.CurrentUser.UserId && n.StartDate.Date == date.Date).ToListAsync();

If you are not getting any records then for that specific combination of userId and Date then the most likely thing is that there aren't any records with that exact combination. 如果没有得到任何记录,则针对userId和Date的特定组合,那么最有可能的是没有任何记录具有该确切的组合。

For some reason, the same query ended up working. 由于某种原因,同一查询最终可以正常工作。 I improved on it by removing the first part, where I was checking for the current user's id. 我删除了第一部分,检查当前用户的ID,对此进行了改进。 That wasn't necessary since in my case. 自从我而言,这不是必需的。

So the final query looks like this: 因此,最终查询如下所示:

   var alerts = await alertTable.Where(n=> n.StartDate >= fromDate && n.StartDate <= toDate).ToListAsync();

This now pulls up all the logged in user's details. 现在,这将拉出所有已登录用户的详细信息。

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

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