文章9
标签7
分类4

记一个扩展方法 还需要优化 目前:能跑就行

/// <summary>

    /// 判断指定类型内是否为空或某属性值是否为空 属性值可以用,分割
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <param name="IsEmpty"></param>
    /// <param name="IsAllNotEmpty"></param>
    /// <param name="AttributeName"></param>
    /// <returns></returns>
    public static bool IsNotEmpty<T>(this List<T> list, bool IsEmpty=true, bool IsAllNotEmpty=true, string AttributeName="")
    {
        if (list == null || list.Count <= 0)
            return false;
        foreach (var item in list)
        {
            var str = JsonConvert.SerializeObject(item);
            var jobj = JsonConvert.DeserializeObject(str) as JObject;
            if (jobj == null)
                return false;

            if (IsAllNotEmpty)
            {
                foreach (var ite in jobj.Children())
                {
                    if (ite != null)
                    {
                        try
                        {
                            var property1 = ite as JProperty;
                            var value = property1.Value;
                            if (value == null || value.ToObject<object>()==null)
                                return false;
                            if (IsEmpty && value.ToString() == string.Empty)
                                return false;
                        }
                        catch (Exception)
                        {
                            return false;
                        }
                    }
                    else 
                    {
                        return false;
                    }
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(AttributeName))
                {
                    return false;
                }
                List<string> vs = new List<string>();
                if (AttributeName.Contains(','))
                {
                    vs = AttributeName.Split(',').ToList();
                }
                else
                {
                    vs.Add(AttributeName);
                }
                foreach (var ite in vs) 
                {
                    var value = jobj.Value<string>(ite);
                    if(value==null)
                        return  false;
                    if (IsEmpty) 
                    {
                        if (value == string.Empty)
                            return false;
                    }
                    
                }
            }
        }
        return true;
    }

    0 评论

    ">