Exemplo n.º 1
0
        /// <summary>
        /// 取得無 DbNull 的欄位值,組成以逗號分隔的字串
        /// </summary>
        /// <param name="rows">資料列陣列</param>
        /// <param name="fieldName">欄位名稱</param>
        /// <param name="checkEmpty">檢查空白</param>
        /// <param name="isSqlStr">是否用單引號包起來</param>
        /// <param name="distinct">是否過濾同樣資料</param>
        /// <returns></returns>
        public static string GetFieldValues(IEnumerable <DataRow> rows, string fieldName, bool checkEmpty = true, bool isSqlStr = true, bool distinct = false)
        {
            string result = string.Empty;

            if (rows.Count() != 0)
            {
                if (HasField(rows.First(), fieldName))
                {
                    foreach (DataRow row in rows)
                    {
                        if (row.RowState != DataRowState.Deleted)
                        {
                            string value = BaseFunc.CStr(GetFieldValueNotDbNull(row, fieldName));
                            if (checkEmpty && StrFunc.StrIsEmpty(value))
                            {
                                continue;
                            }
                            else
                            {
                                value = isSqlStr ? StrFunc.SQLStr(value) : value;
                                if (distinct && StrFunc.StrContains(result, value))
                                {
                                    continue;
                                }
                                result += (StrFunc.StrIsNotEmpty(result) ? "," : "") + value;
                            }
                        }
                    }
                }
            }
            return(result);
        }