/// <summary> /// 根据主键查询 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryable"></param> /// <param name="pkValues">主键集合</param> /// <returns></returns> public static Queryable <T> In <T>(this Queryable <T> queryable, params object[] pkValues) { Check.Exception(pkValues == null || pkValues.Length == 0, "In.pkValues的Count不能为0"); var type = pkValues[0].GetType(); if (type != SqlSugarTool.IntType && type != SqlSugarTool.GuidType && type.FullName.IsCollectionsList()) { var newList = new List <object>(); foreach (var item in (IEnumerable)pkValues[0]) { newList.Add(item); } pkValues = newList.ToArray(); Check.Exception(pkValues == null || pkValues.Length == 0, "In.pkValues的Count不能为0"); } var pkName = SqlSugarTool.GetPrimaryKeyByTableName(queryable.DB, queryable.TableName); queryable.OrderByValue = null; Check.ArgumentNullException(pkName, "In(params object[]PkValue)查询表中不存在主键,请换In的其它重载方法。"); return(queryable.In <T, string>(pkName, pkValues.Select(it => it.ToString()).ToArray())); }
/// <summary> /// 根据主键查询 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryable"></param> /// <param name="pkValue">主键</param> /// <returns></returns> public static T InSingle <T>(this Queryable <T> queryable, object pkValue) { return(queryable.In(pkValue).ToList().SingleOrDefault()); }