Dynamically select columns in runtime using entity framework

前端 未结 5 1269
旧巷少年郎
旧巷少年郎 2020-12-11 05:07

I have an existing function like this

public int sFunc(string sCol , int iId)
{
    string sSqlQuery = \"  select  \" + sCol + \" from TableName where ID = \         


        
5条回答
  •  忘掉有多难
    2020-12-11 05:20

    You could use Reflection, something like this (not tested code):

    public string sFunc(string sCol , int iId)
    {
      var row = TableRepository.Entities.Where(x => x.ID == iId);
      var type = typeof(row);
      var propInfo = type.GetProperty(sCol);
    
      if (propInfo != null)
      {
        string value = (string)propInfo.GetValue(row);
    
        return value;
      }
    
      return null;
    }
    

提交回复
热议问题