【问题标题】:Need Help for Join tables in Linq on ASP.NET Core在 ASP.NET Core 上的 Linq 中连接表需要帮助
【发布时间】:2021-05-07 14:34:54
【问题描述】:

基本上我正在研究 ASP.NET Core,并且我有一个 FromSqlRaw 语句,它过滤并将一些 ids 保留在一个 talbe (ActiveCalls) 的值中。 然后使用 Linq 过滤另一个数据库表(AppUsers)。 我的目标是使用我从 ActiveCalls 获取的 Id 保留所有 Appuser 列。

带有 Ids 结果的 SqlRaw 代码:(13) 正确!

var results = _context.ActiveCalls.FromSqlRaw("SELECT web_userid from ActiveCalls where TimeReceived>'2021-01-01' and GeoX > 23.8097593342164 and GeoX < 23.8838031312548 and GeoY > 38.1026672246045 and GeoY < 38.1607577524088 group by web_userid");

带有数据的 Linq 代码:结果 (2281) 正确!

appusers = appusers.Where(s => s.LastCall >= start && s.LastCall <= end);

加入表的代码,仅保留基于我在 ActiveCalls 上找到的 Id 的 AppUser 记录。结果(10)错误!

 appusers = from res in results
                     join ap in appusers
                     on res.web_userid equals ap.Id
                     select ap;

我必须使用 Linq 和这 3 个步骤获取结果,而不是全部合并到 Sql 查询中

【问题讨论】:

  • 我没有完全理解。想在一个链接短语中总结这三个步骤吗?

标签: c# asp.net linq


【解决方案1】:

我不知道在这种情况下使用FromSqlRaw

var results = 
   from ac in _context.ActiveCalls 
   where ac.TimeReceived > DateTime.Parse("2021-01-01") 
     && ac.GeoX > 23.8097593342164 
     && ac.GeoX < 23.8838031312548 
     && ac.GeoY > 38.1026672246045 
     && ac.GeoY < 38.1607577524088
   select ac.web_userid;

results = results.Distinct();

var appusers = _context.AppUsers.AsQueryable();
appusers = appusers.Where(s => s.LastCall >= start && s.LastCall <= end);

appusers = 
   from res in results
   join ap in appusers on res.web_userid equals ap.Id
   select ap;

如果还是没有正确的结果,说明AppUser.Id不是web_userid

【讨论】:

    【解决方案2】:

    试试这样的东西

    var results = _context.ActiveCalls.FromSqlRaw("SELECT web_userid from ActiveCalls where TimeReceived>'2021-01-01' and GeoX >
    23.8097593342164 and GeoX < 23.8838031312548 and GeoY > 38.1026672246045 and GeoY < 38.1607577524088 group by web_userid");
    
    appusers = appusers.Where(s => s.LastCall >= start && s.LastCall <= end && results.web_userid.contains(s.Id));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多