![](/img/trans.png)
[英]How to access results of dynamic linq query (PredicateBuilder linq to entities) in a partial view in MVC 4?
[英]Dynamic query with linq to entities using PredicateBuilder
我正在尝试在一个非常简单的表上运行查询,但是没有任何结果。 该表包含带有以下列的操作日志:
我想要得到的是ActionTypeID == 1的所有EntryID的列表,并且Person(部门)以动态字符串列表之一结尾。 问题在于,仅应检查操作日志中一个EntryID的第一个操作,以匹配这些部门中的一个。
我知道这听起来很奇怪。 这可能是由于我的英语不好以及我的前同事的数据库设计功能不佳造成的。 我不知道哪个更糟。
我尝试获得想要的结果是这样的:
var predicate = PredicateBuilder.False<ActionLog>();
var pOuter = PredicateBuilder.True<ActionLog>();
pOuter = pOuter.And(h => h.TableName.ToUpper() == "Contact".ToUpper());
pOuter = pOuter.And(h => h.ActionType.ID == 1);
foreach (var dep in b.DepartmentList)
{
predicate = predicate.Or(x => x.Person.EndsWith("/" + dep.Value));
}
pOuter = pOuter.And(predicate.Expand());
mContactIDs = (from h in db.ActionLog
orderby h.Date
select h).AsExpandable().Where(pOuter).Select(x => x.EntryID).Distinct().ToList();
但这会返回所有条目,其中“人员”以所选部门结尾。 我只想使用此EntryID检查第一个条目。
我希望任何人都能理解我要从困惑的德语到奇怪的英语的翻译。
哦,对了,它是.Net 3.5
编辑我认为我在找到正确的解释时遇到一些问题-我将使用一些虚拟数据进行尝试。
ID | ActionTypeID | EntryID | TableName | ControlName | Person | Date
----------------------------------------------------------------------------------------
1 | 1 | 3 | 'Contacts' | NULL | 'xxxx/department1' | 04/11/2012
2 | 1 | 3 | 'Contacts' | NULL | 'yyyy/department2' | 04/11/2012
3 | 1 | 5 | 'Contacts' | NULL | 'yyyy/department2' | 04/13/2012
4 | 1 | 14 | 'Contacts' | NULL | 'zzzz/department1' | 04/16/2012
在我的示例中,我将搜索以下日志条目:“人员以'/ department2'结尾,并首次出现新的EntryID”,“ TableName =='Contacts'”和“ ActionTypeID == 1”。 我要接收的结果是(仅EntryIDs)5,因为这是Department2的用户是第一个插入该条目的唯一条目,该条目以ActionTypeID 1插入。我知道这很愚蠢,如果我设计了数据库,不会这样做,但是现在我必须处理它。 如果要查询的部门数量不是动态的,我将使用以下代码段:
IQueryable<ActionLog> log = db.ActionLog;
mContactIDs = (from k in db.Contacts
where (from h in log
where h.TableName == "Contacts"
&& h.EntryID == k.ID
&& h.ActionType.ID == 1
orderby h.Date
select h).FirstOrDefault().Person.EndsWith("/" + b.Department)
select k.ID).ToList();
但是我不知道如何将这些动态部门与愚蠢的设计(以及为一个条目创建多个ActionTypeID 1(创建))连接。我什至不知道为什么应用程序会保存这个)
谢谢你,本
我认为问题是在foreach循环内捕获的变量值(即dep.Value
)。 复制变量并在表达式中使用它。 这个问题在这里解释。
foreach (var dep in b.DepartmentList)
{
var depValue = dep.Value;
predicate = predicate.Or(x => x.Person.EndsWith("/" + depValue));
}
我决定改用Entity SQL。
List<int> mContactIDs = new List<int>();
using (var db = new Entities())
{
string queryString = @"
SELECT
VALUE H.EntryID
FROM
Entities.ActionLog AS H
WHERE
H.ID IN (
SELECT VALUE TOP (1) Hi.ID
FROM Entities.ActionLog AS Hi
WHERE Hi.ActionType.ID = 1
AND Hi.TableName = 'Kontakt'
AND Hi.EntryID = H.EntryID
ORDER BY Hi.Date
)
{0}
GROUP BY H.EntryID";
List<string> lDepartments = new List<string>();
foreach (var dep in b.DepartmentList)
{
lDepartments.Add(string.Format(" H.Person LIKE '%{0}' ", dep.Value));
}
string sDepartmentQuery = string.Empty;
if (lDepartments.Count > 0)
{
sDepartmentQuery = string.Format(" AND ({0}) ", string.Join(" OR ", lDepartments.ToArray()));
}
var result = db.CreateQuery<int>(string.Format(queryString, sDepartmentQuery));
if (result != null && result.Count() > 0)
{
mContactIDs = result.ToList();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.