using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;
using System.Collections.Generic;
namespace IRaCIS.Core.Infrastructure.Extention
{
    public static class IQueryableExtensions
    {
       /// 
       /// 获取第一条 为null提示
       /// 
       /// 
       /// 
       /// 
       /// 
        public  static async Task FirstNotNullAsync(this IQueryable source)
        {
            var result =await source.FirstOrDefaultAsync();
            if (result == null)
            {
                throw new QueryBusinessObjectNotExistException($"The query object {typeof(TSource).Name} does not exist in database, Please check the query parameters");
            }
            else
            {
                return result;
            }
        }
        /// 
        /// 查询字符串 为null 返回string.Empty
        /// 
        /// 
        /// 
        /// 
        public static  string FirstIsNullReturnEmpty(this IEnumerable source)
        {
            var result =  source.FirstOrDefault();
            if (result == null || result == string.Empty)
            {
                return string.Empty;
            }
            else
            {
                return result;
            }
        }
        /// 
        /// 查询字符串 为null 返回指定字符
        /// 
        /// 
        /// 
        /// 
        public static string FirstIsNullReturnValue(this IEnumerable source,string value)
        {
            var result = source.FirstOrDefault();
            if (result == null || result == string.Empty)
            {
                return value;
            }
            else
            {
                return result;
            }
        }
    }
}