根据类型搜索 LINQ 实体的通用方法
partial void UpdateDenomLimit(DenomLimit instance)
{
var oldData = DataClassesDataContext.DenomLimits.Where(b => b.ID == instance.ID).First();
//Code that logs the audit when instance and oldData is passed to it
LogAudit(oldData, instance);
//Code that updates the instance
this.ExecuteDynamicUpdate(instance);
}
以上是更新数据库中 DenomLimit 实例的方法。 我正在记录对实体所做的更改的审核。为此,我通过以下代码获取实例的先前状态,并且它工作正常:
var oldData = DataClassesDataContext.DenomLimits.Where(b => b.ID == instance.ID).First();
现在我需要一个通用 LINQ 查询,它可以在向其传递三个参数时获取 oldData: 1. 任意类型的实例 2.主键列名 3. 传递的实例的主键列的值。
...这样我就可以将该代码保留在 LogAudit 中,然后不需要在每个函数中获取。
LINQ 查询可能如下所示:
var oldData = DataClassesDataContext.GetTable<instance>().Where("b => b." + colName + " == @0", new object[] { id }).First();
partial void UpdateDenomLimit(DenomLimit instance)
{
var oldData = DataClassesDataContext.DenomLimits.Where(b => b.ID == instance.ID).First();
//Code that logs the audit when instance and oldData is passed to it
LogAudit(oldData, instance);
//Code that updates the instance
this.ExecuteDynamicUpdate(instance);
}
Above is a method for updating an instance of DenomLimit in the database.
I am logging an audit of what changes are done to the entity. For that I get the previous state of the instance by the following code and it works fine:
var oldData = DataClassesDataContext.DenomLimits.Where(b => b.ID == instance.ID).First();
now I need a generic LINQ query which can fetch the oldData when three parameters are passed to it:
1. an instance of any type
2. A primary key column name
3. A value of primary key column for the passed instance.
...so that I can keep that code in the LogAudit and then do not need to fetch in every function.
A LINQ query which will possibly look like:
var oldData = DataClassesDataContext.GetTable<instance>().Where("b => b." + colName + " == @0", new object[] { id }).First();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此处使用完整的命名空间来指示它来自的命名空间,以便更轻松地合并到代码中。
在本例中,使用 Func 的表达式允许 EF 找出您正在查找的数据。
以下是如何使用它的示例:
The full namespace is used here to indicate the namespace that it came from to make it easier to incorporate into your code.
In this case using an Expression of a Func allows EF to figure out what data you are looking for.
Here is an example of how it would be used: