• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C#List<T> 泛型查询数据和设置值

武飞扬头像
xcanel
帮助1

一般情况下List<T>中的T,都是已知的对象(即给定的对象)。所以使用.Select .FirstOrDefault查询起来很方便。网上也有很多这样的栗子。

我碰到的问题就是泛型对象中查询和设置值。网上没搜到查询方法。就自己写了个处理方式。

1. 在UtilConvert类中定义了2个方法:

  1.  
    public static class UtilConvert
  2.  
    {
  3.  
    /// <summary>
  4.  
    /// 获取对象字段的值
  5.  
    /// </summary>
  6.  
    /// <param name="obj"></param>
  7.  
    /// <param name="fieldID"></param>
  8.  
    /// <returns></returns>
  9.  
    public static string ObjGetFieldValue(this object obj,string fieldID)
  10.  
    {
  11.  
    Type type = obj.GetType();
  12.  
    PropertyInfo[] propertyInfos = type.GetProperties();
  13.  
    foreach (PropertyInfo item in propertyInfos)
  14.  
    {
  15.  
    if (item.Name.ToUpper() == fieldID.ToUpper())
  16.  
    return (item.GetValue(obj, null) == null ? "" : item.GetValue(obj, null)).ToString();
  17.  
    }
  18.  
    return "";
  19.  
    }
  20.  
     
  21.  
    /// <summary>
  22.  
    /// 设置对象字段的值
  23.  
    /// </summary>
  24.  
    /// <param name="obj">Class对象</param>
  25.  
    /// <param name="fieldID">字段</param>
  26.  
    /// <param name="value"></param>
  27.  
    /// <returns></returns>
  28.  
    public static bool ObjSetFieldValue(this object obj, string fieldID,string value)
  29.  
    {
  30.  
    Type type = obj.GetType();
  31.  
    PropertyInfo[] propertyInfos = type.GetProperties();
  32.  
    foreach (PropertyInfo item in propertyInfos)
  33.  
    {
  34.  
    if (item.Name.ToUpper() == fieldID.ToUpper())
  35.  
    {
  36.  
    item.SetValue(obj, string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, item.PropertyType), null);
  37.  
    return true;
  38.  
    }
  39.  
    }
  40.  
    return false;
  41.  
    }
  42.  
    }
学新通

2. 具体的实现方法:

  1.  
    #region 同步SFA数据
  2.  
    /// <summary>
  3.  
    /// 同步SFA数据
  4.  
    /// </summary>
  5.  
    /// <typeparam name="T"></typeparam>
  6.  
    /// <param name="sql"></param>
  7.  
    /// <returns></returns>
  8.  
    private async Task<bool> AsyncSFADate<T>(string sql) where T : class, new()
  9.  
    {
  10.  
    try
  11.  
    {
  12.  
    SFARequestHelper sfaHelper = new SFARequestHelper();
  13.  
     
  14.  
    //最后修改日期
  15.  
    string LastModifiedDate = "2022-05-01T07:37:50.000 0000";
  16.  
    var list_temp = await _dal.Query<T>("SEP_MSSQL_SFA_BK", "select * from " typeof(T).Name);
  17.  
    if (list_temp != null && list_temp.Count > 0)
  18.  
    LastModifiedDate = list_temp.Max(o => o.ObjGetFieldValue("LastModifiedDate"));
  19.  
     
  20.  
    if (!sql.ToUpper().Contains(" WHERE"))
  21.  
    sql = sql " WHERE LastModifiedDate>=" LastModifiedDate;
  22.  
    else
  23.  
    sql = sql " and LastModifiedDate>=" LastModifiedDate;
  24.  
    sql = sql " order by LastModifiedDate ";
  25.  
     
  26.  
    var ContractList = await sfaHelper.QueryDataList<T>(sql);
  27.  
    if (ContractList.Count > 0)
  28.  
    {
  29.  
    var IDs = ContractList.Select(o => o.ObjGetFieldValue("Id")).ToList();
  30.  
     
  31.  
    _unitOfWork.BeginTran();
  32.  
     
  33.  
    var list = await _dal.Query<T>("SEP_MSSQL_SFA_BK", "select * from " typeof(T).Name " where Id in (" IDs.ListToString(true) ")");
  34.  
    //var list = await _dal.Query<T>(o => IDs.Contains(o.ObjGetFieldValue("Id"))); //拉姆达不支持函数:ObjGetFieldValue
  35.  
    //数据同步到OA
  36.  
    List<T> insertList = new List<T>(); //新增
  37.  
    List<T> updateList = new List<T>(); //更新
  38.  
     
  39.  
    foreach (var item in ContractList)
  40.  
    {
  41.  
    var info = list.FirstOrDefault(o => o.ObjGetFieldValue("Id").ToUpper() == item.ObjGetFieldValue("Id").Trim().ToUpper());
  42.  
    if (info == null)
  43.  
    {
  44.  
    //新增数据
  45.  
    info = new T();
  46.  
    ModelMapHelper.GetModelMerge<T, T>(item, info);
  47.  
    info.ObjSetFieldValue("AutoID", "0");
  48.  
    info.ObjSetFieldValue("IsDelete", ((int)AppEnum.YNStatus.No).ToString());
  49.  
    info.ObjSetFieldValue("CreateTime", DateTime.Now.ToString(AppConst.FormateDateTime));
  50.  
     
  51.  
    insertList.Add(info);
  52.  
    }
  53.  
    else
  54.  
    {
  55.  
    //更新数据
  56.  
    if (item.ObjGetFieldValue("IsDelete") == ((int)AppEnum.YNStatus.No).ToString())
  57.  
    {
  58.  
    info.ObjSetFieldValue("AutoID", item.ObjGetFieldValue("AutoID"));
  59.  
    info.ObjSetFieldValue("IsDelete", item.ObjGetFieldValue("IsDelete"));
  60.  
    info.ObjSetFieldValue("CreateTime", item.ObjGetFieldValue("CreateTime"));
  61.  
    info.ObjSetFieldValue("UpdateTime", DateTime.Now.ToString(AppConst.FormateDateTime));
  62.  
    ModelMapHelper.GetModelMerge<T, T>(item, info);
  63.  
    updateList.Add(info);
  64.  
    }
  65.  
    }
  66.  
    }
  67.  
     
  68.  
    if (insertList.Count > 0)
  69.  
    await _dal.Add<T>(insertList);
  70.  
    if (updateList.Count > 0)
  71.  
    await _dal.Update<T>(updateList);
  72.  
    _unitOfWork.CommitTran();
  73.  
    }
  74.  
     
  75.  
    return true;
  76.  
    }
  77.  
    catch (Exception exp)
  78.  
    {
  79.  
    _logger.LogError(exp, "对象:" typeof(T).Name " Error:" exp.Message);
  80.  
    }
  81.  
    _unitOfWork.RollbackTran();
  82.  
    return false;
  83.  
     
  84.  
    }
  85.  
    #endregion
学新通

说明下,我是在Salesforce的同步中实现的,主要是把Salesforce中数据定时同步到下来。

SFARequestHelper是辅助类,这里不详讲了。

主要是这里使用方式:

学新通

  注意拉姆达不支持。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfkbic
系列文章
更多 icon
同类精品
更多 icon
继续加载