[英]using Linq - get groups from db table where one record in each group matches a search
I am in search of assistance in working out how to perform the request that I need using Linq to Entities. 我正在寻求帮助,以制定出如何使用Linq to Entities执行所需的请求。
I have a table in an IBM DB2 database which, simplified, looks like this: PK {Integer}
, GROUP {CHAR FOR BIT DATA [16]}
, USERNAME {CHAR[15]}
, ACTION {INTEGER}
. 我在IBM DB2数据库中有一个简化的表,看起来像这样:
PK {Integer}
, GROUP {CHAR FOR BIT DATA [16]}
, USERNAME {CHAR[15]}
, ACTION {INTEGER}
。 (none of the values can be null) (任何值都不能为空)
There are more columns but these will do to get the point across. 有更多的专栏,但可以做得很清楚。 What I need to do is such:
我需要做的是这样的:
I get user input requesting all records with a specific ACTION
or USERNAME
or both. 我收到用户输入,要求使用特定的
ACTION
或USERNAME
或两者都记录所有记录。
What I need to return is all records grouped by GROUP
which contain the requested ACTION
and USERNAME
(if one of them is empty, it is ignored and I match the other). 我需要返回的是按
GROUP
分组的所有记录,其中包含请求的ACTION
和USERNAME
(如果其中一个为空,则将其忽略,而我匹配另一个)。
I've tried a number of things but I keep running into walls with Linq to Entities. 我已经尝试了很多方法,但是我一直使用Linq to Entities碰壁。 I get errors like
"SQL0338N An ON clause associated with a JOIN [...]"
and There is no stored type corresponding to the EDM Type 'Edm.Binary' of primitive 'Binary'
. 我收到类似
"SQL0338N An ON clause associated with a JOIN [...]"
错误,并且There is no stored type corresponding to the EDM Type 'Edm.Binary' of primitive 'Binary'
。
I've tried to do a select where I 我试图做一个选择
The moment I try to run .ToArray()
I get smacked with an Exception, either DB2 exception or Linq exception (depending on the changes I made to the code).' 当我尝试运行
.ToArray()
的那一刻,我被一个DB2异常或Linq异常(取决于我对代码所做的更改.ToArray()
。
I would very much appreciate any assistance in figuring out a viable way to do this. 我非常感谢您提供协助,以找出可行的方法。
** Edit ** **编辑**
@Alex Well... the thing is that I tried several different ways of approaching the code and they each led to one of the same two errors (see above). @Alex好...问题是我尝试了几种不同的方法来处理代码,并且每种方法均导致相同的两个错误之一(请参见上文)。 The conclusion I came to was that the attempts I made were not just syntactically flawed but required a change to the direction with which I was approaching the problem.
我得出的结论是,我所做的尝试不仅在语法上存在缺陷,而且还需要改变解决问题的方向。
@ZacharyKniebel This is the solution! @ZacharyKniebel这是解决方案! Thank you!
谢谢! The only thing is - the example you gave would only return the groups that have a record where both fields are matched.
唯一的是-您给出的示例将仅返回具有两个字段都匹配的记录的组。 To make it so that it is one, the other, or both, I used this:
为了使它成为一个,另一个或两者,我使用了以下方法:
...
var records = db.Records
.GroupBy(r => r.Group);
if(requestedAction != null)
{
records = records.Where(g => g.Any(r => r.Action == requestedAction));
}
if(!string.isNullOrWhiteSpace(requestedUsername))
{
records = records.Where(g => g.Any(r => r.Username.ToLower().Contains(requestedUsername.ToLower()));
}
...
Try this: 尝试这个:
...
var records = db.Records
.GroupBy(r => r.Group)
.Where(g => g.Any(r => r.Action == requestedAction &&
r.Username == requestedUsername)));
...
Here, is a breakdown of the above: 以下是上述内容的细分:
Note that Linq will parse the Linq query into SQL before actually running the query. 请注意,Linq将在实际运行查询之前将Linq查询解析为SQL。 As such, the entirety of the above will be done in a single query.
这样,以上的全部将在单个查询中完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.