简体   繁体   English

如何将实体对象传递给C#中的函数

[英]How do I pass an Entity Object to a function in C#

I have a public function that queries against a specific entity. 我有一个查询特定实体的公共功能。 I would like to replicate the function for any table I pass in but don't know how. 我想为传入的任何表复制该函数,但不知道如何。 Here is the working function I want to make dynamic: 这是我要动态化的工作功能:

public string MaxDepartment()
    {
        CPLinkEntities _context = new CPLinkEntities();
        results = _context.LOG_Departments.Max(t => t.LastUpdated); // hard coded
        string hex = BitConverter.ToString(results);
        hex =  hex.Replace("-", "");
        return hex;
    }

What I would really like to do here is pass in an entity to query against. 我在这里真正想做的是传递一个实体以进行查询。 All entities have a timestamp. 所有实体都有时间戳。 Here is what I envision it would look like but doesn't work: 这是我设想的样子,但无法正常工作:

 public string MaxDepartment(CPLinkEntities tableName)
    {

        var results = tableName.Max(t => t.LastUpdated);
        string hex = BitConverter.ToString(results);
        hex =  hex.Replace("-", "");
        return hex;
    }

Calling the function from controller then would be: 然后从控制器调用该函数将是:

CPLinkEntities context = new CPLinkEntities();
var tableName = context.LOG_Departments;
var maxDept = cf.MaxDepartment(tableName); 

The easiest way to do it without changing any of your existing classes (if you can, see Oleksii's answer ) is to manually create the expression tree and have it select the property you want. 在不更改任何现有类的情况下(如果可以的话,请参见Oleksii的答案 ),最简单的方法是手动创建表达式树并让其选择所需的属性。

public static string MaxDepartment<U>(IQueryable<U> table)
{
    var parameter = Expression.Parameter(typeof(U));
    var property = Expression.Property(parameter, "LastUpdated");
    var lambada = Expression.Lambda<Func<U, byte[]>>(property, parameter);

    var results = table.Max(lambada);
    string hex = BitConverter.ToString(results);
    hex = hex.Replace("-", "");
    return hex;
}

You would call it via 您可以通过以下方式致电

using(CPLinkEntities _context = new CPLinkEntities()) //You forgot to dispose in your original example
{
    var max = MaxDepartment(_context.LOG_Departments);

    //Do whatever you want with max here.
}

This will fail at runtime if you try to pass in a table that does not have a LastUpdated property. 如果尝试传递不具有LastUpdated属性的表,则此操作将在运行时失败。

I think you should mark your entity with an interface like this: 我认为您应该使用这样的界面标记您的实体:

public interface ILastUpdatable
{
   byte[] LastUpdated {get;set;}
}

public partial class LOG_Departments : ILastUpdatable
{
}

and then make your method expecting an object of type that implements an interface like this: 然后让您的方法期望一个实现如下接口的类型的对象:

public string MaxDepartment<TLastUpdatable>(IQueryable<TLastUpdatable> updatables)
  where TLastUpdatable : class, ILastUpdatable
{
    var results = updatables.Max(t => t.LastUpdated);
    string hex = BitConverter.ToString(results);
    hex =  hex.Replace("-", "");
    return hex;
}

UPDATE: Also you would consider to use it as extension method: 更新:您也可以考虑将其用作扩展方法:

public static class MaxUpdatableExtensions
{
    public static string MaxDepartment<TLastUpdatable>(this IQueryable<TLastUpdatable> updatables)
      where TLastUpdatable : class, ILastUpdatable
    {
        var results = updatables.Max(t => t.LastUpdated);
        string hex = BitConverter.ToString(results);
        hex =  hex.Replace("-", "");
        return hex;
    }
}

and call it like this: 并这样称呼它:

CPLinkEntities _context = new CPLinkEntities();
var results = _context.LOG_Departments.MaxDepartment();

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

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