转换表达式< Func< DTOUser,bool>>谓词表达< Func< User,bool>>>谓词 [英] Convert Expression<Func<DTOUser, bool>> predicate to Expression<Func<User, bool>> predicate

查看:133
本文介绍了转换表达式< Func< DTOUser,bool>>谓词表达< Func< User,bool>>>谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表达式中有问题

我有一个实体

public class User{ 
public string Username{get;set;}
  public int PhoneNumber{get;set;}
  public string FIrstName{get;set;}
  public string LastName{get;set;}
}

我有一个DTO

public class DTOUser{
  public string Username{get;set;}
  public int PhoneNumber{get;set;}
  public string FIrstName{get;set;}
  public string LastName{get;set;}
  }

然后我有一个代码片段通用

Then I have a snippet code generic

 public IList<DTOUser> SelectAll(Expression<Func<DTOUser, bool>> predicate)
    {
           using (var adc = _conn.GetContext())
        {   
       // what should I do ?? so i can convert Predciate to get all values from Users (Entity)   
//it generates an error because predicate can't be cast into Entity User                    
 //   var users = adc.Users.All(predicate);
       }          
    }

我想通过传递LAMBDA获取DTOUser的列表表达式

I wanted to get a list of DTOUser by Passing LAMBDA Expression

accountrepo.SelectAll( user => user.firstname.equals ("sample"));

我已经研究了这个问题,得出结论,因为DTOUser和User是不同的类型,

I have researched this issue and come to the conclusion that because DTOUser and User are of different type, it's difficult to cast the Expression from one type to another.

Jon Skeet已经提出了一个解决方案:

One solution has been suggested by Jon Skeet:

如何转换表达式< Func< T,DateTime>>到表达< Func< T,对象>

但是,由于这个解决方案似乎我必须将每个值从DTOUser映射到用户不是这个使其更复杂,因为我的DTOUser包含超过15个属性。

but as this solution seems that I have to map each value from DTOUser to User doesn't this make it more complex as my DTOUser contains more than 15 properties.

有人可以帮助我吗?

推荐答案

您不能直接从一种类型转换到另一种类型,您可以:

You cannot cast directly from one type to another, you can do:


  1. 手动映射

  2. 自动使用反射(由于名称相同)

  3. 使用 AutoMapper

  1. a manual mapping
  2. map automatically using reflection (as propery names are the same)
  3. Use AutoMapper

对于使用反射的映射,可以使用以下通用代码:

For mapping using reflection, you can use the following generic code :

public static T1 CopyProperties<T1, T2>(T2 model)
    where T1 : new()
    where T2 : new()
{
    // Get all the properties in the model
    var type = model.GetType();
    var properties = type.GetProperties();

    var result = new T1();
    var resultType = result.GetType();
    var resultProperties = resultType.GetProperties();

    // Loop through each property
    foreach (var property in properties)
    {
        var resultProperty = resultProperties.FirstOrDefault(n => n.Name == property.Name && n.PropertyType == property.PropertyType);
        if (resultProperty != null)
        {
            resultProperty.SetValue(result, property.GetValue(model, null), null);
        }
    }
    return result;
}

它将复制相同类型和名称的属性

It will copy properties with the same types and names

这篇关于转换表达式&lt; Func&lt; DTOUser,bool&gt;&gt;谓词表达&lt; Func&lt; User,bool&gt;&gt;&gt;谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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