如何使用数据库中的特定列获取并将其存储在使用通用存储库模式的变量中

时间:2019-06-15 10:41:24

标签: entity-framework linq repository-pattern unit-of-work generic-repository

我正在使用此功能从通用存储库中的数据库中获取数据

public ICollection<TType> GetSpecificColumn<TType> 
   (Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, 
    TType>> select) where TType : class
    {
        return DbSet.Where(where).Select(select).ToList();
    }

但是当我返回列表时,它给出了错误,我的服务代码是

public ICollection<Yarn_Blends> GetYarnBlendIdByYarnId(long yarnId)
    {
        var myData = _unitOfWorks.Yarn_Repository.GetSpecificColumn(x => x.YarnID == yarnId, x => new { x.YarnBlendID });
        var blend = myData.First().ToString();
        var yarnBlenIdGet = blend.Remove(0,16);
        var blendId = yarnBlenIdGet.Remove(6);
        long yarnBlendId = Convert.ToInt32(blendId);
         _unitOfWorks.Yarn_Blend_Repository.GetSpecificColumn(x => x.YarnBlendID == yarnBlendId,x => new {x.YarnBlendName});
        var myyarnBlendId = _unitOfWorks.Yarn_Blend_Repository.GetSpecificColumn(x => x.YarnBlendID == yarnBlendId,x => new {x.YarnBlendName});

        return myyarnBlendId.ToList();
    }

给出错误 无法将类型'System.Collections.Generic.List'隐式转换为'System.Collections.Generic.IEnumerable'。存在显式转换(您是否缺少演员表?)

1 个答案:

答案 0 :(得分:0)

我将代码更改为

public object GetYarnBlendIdByYarnId(long yarnId)
    {
        var myData = _unitOfWorks.Yarn_Repository.GetSpecificColumn(x => x.YarnID == yarnId, x => new {x.YarnBlendID });
        var blend = myData.First().ToString();
        var yarnBlenIdGet = blend.Remove(0,16);
        var blendId = yarnBlenIdGet.Remove(6);
        long yarnBlendId = Convert.ToInt32(blendId);
        IEnumerable<Yarn_Blends> list = _unitOfWorks.Yarn_Blend_Repository.GetMany(p => p.YarnBlendID == yarnBlendId);

        object myyarnBlendId = _unitOfWorks.Yarn_Blend_Repository.GetSpecificColumn(x => x.YarnBlendID == yarnBlendId,x => new {x.YarnBlendName,x.YarnBlendID});
        //foreach(var myYarnData in myyarnBlendId)
        //{
        //    string YarnBlendName = myYarnData.YarnBlendName;
        //}
       // return list.ToList();
        return myyarnBlendId;
    }

现在可以正常使用