如何使用Expression LINQ创建功能 [英] How to create the function using Expression linq

查看:94
本文介绍了如何使用Expression LINQ创建功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的类

public class ProdutoTipo : IAuditable
{
    public Guid ID { get; set; }

    public string Nome { get; set; }
    public string MiniNome { get; set; }
    public string Descricao { get; set; }
    public string Link { get; set; }
    public int? Ordem { get; set; }

    public virtual Foto ImagemExibicao { get; set; }
    public virtual ICollection<ProdutoCategoria> Categorias { get; set; }

    public DateTime CreatedAt { get; set; }
    public string CreatedBy { get; set; }
    public DateTime? UpdatedAt { get; set; }
    public string UpdatedBy { get; set; }

    public bool PaginaInicial { get; set; }

    public ProdutoTipo() { ID = Guid.NewGuid(); }
}



我需要的搜索信息库,并返回true或false <函数BR >
但是这种搜索可以使用类的任何领域!

public bool Existe<TProperty, TComparer>(Expression<Func<ProdutoTipo, TProperty>> entityExpression, TComparer valor)
{
    return Repository.ProdutoTipos.Any(p => /*entityExpression == valor ?????*/);
}



想用这样的...

Existe(p => p.Nome, "Value to comparer!");

感谢大家!

推荐答案

我认为你正在寻找

Func<ProdutoTipo, TProperty> getter = entityExpression.Compile();
Repository.ProdutoTipos.Any(p => getter(p).Equals(valor)); 



但你不妨这样做:

But you might as well do this:

public bool Existe<TProperty, TComparer>(Expression<Func<ProdutoTipo, bool>> expression)  
{  
    return Repository.ProdutoTipos.Any(expression);  
}

和调用:

Existe(p => p.Nome == "Value to comparer!");  

这篇关于如何使用Expression LINQ创建功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆