简体   繁体   English

如何将此查询转换为 linq?

[英]How to translate this query into linq?

I have this SQL query我有这个 SQL 查询

SELECT TOP 1 
    DOCID, ACTOR_ID, ACTOR_ROLE, ACTION_ID 
FROM
    ex_DOCAction 
WHERE
    DOCID = 126251
GROUP BY
    DOCID, ACTOR_ROLE, ACTION_ID, ACTOR_ID
HAVING
    COUNT(ACTION_ID) = 1

I came up with this:我想出了这个:

from x in _db.ex_DOCAction.Where(a => a.DOCID == request.DocId)
group x by new { x.DOCID, x.ACTOR_ROLE, x.ACTION_ID, x.ACTOR_ID } into grouped
where 

and I don't know how to translate而且我不知道怎么翻译

 HAVING COUNT(ACTION_ID) = 1

There are lots of similar questions here, but I didn't find any question where count() had a parameter.这里有很多类似的问题,但我没有发现count()有参数的问题。

I guess you can't translate that group by + uniqueness check directly into SQL.我想您无法将该组 by + 唯一性检查直接转换为 SQL。 You can return a structure, where you calculate the amount of the unique Action_Id s您可以返回一个结构,在其中计算唯一Action_Id的数量

from x in _db.ex_DOCAction.Where(a => a.DOCID == request.DocId)
  group x by new { x.DOCID, x.ACTOR_ROLE, x.ACTION_ID, x.ACTOR_ID }
    into grouped
  select new {Key = grouped.Key, Count = (from y in grouped select y.ACTION_ID).Distinct().Count() };

A Group by create a two dimensional list: List>. A Group by 创建一个二维列表:List>。 To Top is the first member of List. To Top 是 List 的第一个成员。 See code below请参阅下面的代码

var results = _db.ex_DOCAction
   .Where(a => (a.DOCID == request.DocId) && (a.ACTION_ID != null))
   .GroupBy(x => new { x.DOCID, x.ACTOR_ROLE, x.ACTION_ID, x.ACTOR_ID })
   .Select(x => x.First())
   .ToList();

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

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