Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有继承基类的LINQ通用查询?_C#_.net_Linq_Linq To Sql_Generics - Fatal编程技术网

C# 具有继承基类的LINQ通用查询?

C# 具有继承基类的LINQ通用查询?,c#,.net,linq,linq-to-sql,generics,C#,.net,Linq,Linq To Sql,Generics,我试图为我的实体编写一些通用LINQ查询,但在做更复杂的事情时遇到了问题。现在,我正在使用一个EntityDao类,它让我的所有泛型和每个对象类Dao(如Dao)继承它,例如: using LCFVB.ObjectsNS; using LCFVB.EntityNS; namespace AccomplishmentNS { public class AccomplishmentDao : EntityDao<Accomplishment>{} } 不管默认值是nhiberna

我试图为我的实体编写一些通用LINQ查询,但在做更复杂的事情时遇到了问题。现在,我正在使用一个EntityDao类,它让我的所有泛型和每个对象类Dao(如Dao)继承它,例如:

using LCFVB.ObjectsNS;
using LCFVB.EntityNS;

namespace AccomplishmentNS
{
  public class AccomplishmentDao : EntityDao<Accomplishment>{}
}
不管默认值是nhibernate命令,我应该如何为LINQ执行此操作

编辑:

getOne即使只是脱离基类(这是自动生成的LINQ类的一部分类),也似乎不起作用。我甚至删除了泛型。我试过:

namespace ObjectsNS
{    
    public partial class Accomplishment
     {
       public Accomplishment getOneByWhereClause(Expression<Action<Accomplishment, bool>> singleOrDefaultClause)
     {
        Accomplishment entity = new Accomplishment();
         if (singleOrDefaultClause != null) {
              LCFDataContext lcfdatacontext = new LCFDataContext();

                 //Generic LINQ Query Here
              entity = lcfdatacontext.Accomplishments.SingleOrDefault(singleOrDefaultClause);

               lcfdatacontext.Dispose();
               }
            return entity;
               }
          }
      }
好的,没问题,我换了:

public Accomplishment getOneByWhereClause(Expression<Action<Accomplishment, bool>> singleOrDefaultClause)
它不起作用,它说x没有声明

我也试过了

 getOne<Accomplishment>
 Expression<Func<
 Expression<Action<

=(

如果我理解你想要什么,你链接的问题描述了(某种程度上)你需要做什么

public ImplementationType getOne(Expression<Func<ImplementationType , bool> singleOrDefaultClause)
{ 
    ImplementationType  entity = null;
    if (singleOrDefaultClause != null) 
    {

        LCFDataContext lcfdatacontext = new LCFDataContext();

        //Generic LINQ Query Here
        entity = lcfdatacontext.GetTable<ImplementationType>().SingleOrDefault(singleOrDefaultClause);


        lcfdatacontext.Dispose();
    }


    return entity;
}
我还没有编译过这个,所以我不能说它100%正确,但是这个想法是可行的。我使用了一些类似的东西,除了我定义的方法是泛型的,带有一个基类来实现公共代码

更新 你不能用new来创建你所需要的类的实例吗?如果你需要更通用的东西,那么我认为你必须用反射来调用构造函数。如果我误解了你的要求,很抱歉

更新评论以获取更多详细信息 更新POCO的扩展:有很多方法可以做到这一点,但有一种方法是从表达式中获取PropertyInfo并调用setter(可能是更好的方法,但我还没有找到)。例如,它可能看起来像:

protected internal bool Update<TRet>(Expression<Func<T, TRet>> property, TRet updatedValue)
{
    var property = ((MemberExpression)member.Body).Member as PropertyInfo;
    if (property != null)
    {
        property.SetValue(this, updatedValue, null);
        return true;
    }
    return false;
}

Hmmmm在不从函数调用中传入谓词的情况下是否可以这样做?这是我真正想要避免的。主要问题是,我的DAO类没有名为ID的成员。我喜欢将我的对象类与数据库逻辑类(DAO)和业务逻辑类(服务)分开。这似乎有点奇怪。是否没有表达式或.command来表示以字符串形式输入列名/属性?实际上,指向这一点的谓词可以是范围向下延伸到单个实体的任何内容。例如x=>x.name==“some name”&&x.created==“02/03/2010”只要谓词返回单个实体,它就对谓词有效。如果您想要单独的DAO和服务类,那么在任何情况下都必须转换逻辑。如果您想要使用列名和值,我不确定您将如何操作。是否可以根据字符串参数构建谓词?我猜没有since x.id中的id是一个已知的属性。如果可能的话,我想这将是一个两步的过程,根据提供的参数构建一个谓词,然后将该谓词放入上面的示例中。哦,我想我找到了一个答案:看起来正是我需要的,字符串作为参数!Geek,你能扩展一下你关于u的更新吗sing reflection?我让我的方法工作了,但我更喜欢你的方法,因为它更灵活,允许多个参数,但我真的很想把它保留在我的DAO中。如果我的DAO中只有一个db方法,那会有点混乱。将函数设置为getOne会起作用吗?
public Accomplishment getOneByWhereClause(Expression<Func<Accomplishment, bool>> singleOrDefaultClause)
Accomplishment accomplishment = new Accomplishment();
var result = accomplishment.getOneByWhereClause(x=>x.Id = 4)
 getOne<Accomplishment>
 Expression<Func<
 Expression<Action<
Error   1   Operator '>=' is not defined for types 'LCFVB.ObjectsNS.Accomplishment' and 'Integer'.  
public ImplementationType getOne(Expression<Func<ImplementationType , bool> singleOrDefaultClause)
{ 
    ImplementationType  entity = null;
    if (singleOrDefaultClause != null) 
    {

        LCFDataContext lcfdatacontext = new LCFDataContext();

        //Generic LINQ Query Here
        entity = lcfdatacontext.GetTable<ImplementationType>().SingleOrDefault(singleOrDefaultClause);


        lcfdatacontext.Dispose();
    }


    return entity;
}
//note assumption that ConcreteClass DAO has member called Id
var myEntity = ConcreteClass.getOne(x=>x.Id == myIdVariable);
protected internal bool Update<TRet>(Expression<Func<T, TRet>> property, TRet updatedValue)
{
    var property = ((MemberExpression)member.Body).Member as PropertyInfo;
    if (property != null)
    {
        property.SetValue(this, updatedValue, null);
        return true;
    }
    return false;
}
public interface ISession : IDisposable
{
    bool CanCreate<T>() where T : class,IModel;
    bool CanDelete<T>() where T : class, IModel;
    bool CanEdit<T>() where T : class, IModel;
    bool CanGet<T>() where T : class, IModel; 

    T Create<T>(IEditable<T> newObject) where T:class,IModel;

    bool Delete<T>(Expression<Func<T, bool>> selectionExpression) where T : class, IModel;

    PageData<T> Get<T>(int page, int numberItemsPerPage, Expression<Func<T, bool>> whereExpression) where T : class, IModel;
    PageData<T> Get<T, TKey>(int page, int numberItemsPerPage, Expression<Func<T, bool>> whereExpression, Expression<Func<T, TKey>> orderBy, bool isAscending) where T : class, IModel;

    T Get<T>(Expression<Func<T, bool>> selectionExpression) where T : class, IModel;

    IEnumerable<T> GetAllMatches<T>(Expression<Func<T, bool>> whereExpression) where T : class, IModel;

    IEditable<T> GetForEdit<T>(Expression<Func<T, bool>> selectionExpression) where T : class, IModel;

    IEditable<T> GetInstance<T>() where T : class, IModel;

    IQueryable<T> Query<T>() where T : class, IModel;

    bool Update<T>(IEditable<T> updatedObject) where T : class, IModel;
}