Common sample code sharing for C#List to implement row to column conversion (picture)
黄舟
Release: 2017-03-17 11:30:56
Original
3349 people have browsed it
This article introduces System.Linq.Dynamic by converting rows to columns, and introduces the filtering function, which has a good reference value. Let's take a look at the following with the editor
The recent demand for report statistics involves row-to-column reports. Based on past experience, it can be done relatively easily using SQL. This time I decided to take the challenge of converting rows and columns directly through code. Several problems encountered during the period and new knowledge used are summarized and recorded here.
Reading Contents
Introduction to the problem
Dynamic Linq
##System.Linq.DynamicOther usage
Summary
Introduction to the problem
With family For example, monthly expenses can be grouped in any combination of the three dimensions [Name, Area, Month], and one of the three dimensions can be selected as a column display.
/// <summary>
/// 家庭费用情况
/// </summary>
public class House
{
/// <summary>
/// 户主姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 所属行政区域
/// </summary>
public string Area { get; set; }
/// <summary>
/// 月份
/// </summary>
public string Month { get; set; }
/// <summary>
/// 电费金额
/// </summary>
public double DfMoney { get; set; }
/// <summary>
/// 水费金额
/// </summary>
public double SfMoney { get; set; }
/// <summary>
/// 燃气金额
/// </summary>
public double RqfMoney { get; set; }
}
/// <summary>
/// 通过查询条件,获取参数化查询SQL
/// </summary>
/// <param name="gridParam">过滤条件</param>
/// <returns>过滤条件字符</returns>
private static EFFilter GetParameterSQL<T>(QueryCondition gridParam)
{
EFFilter result = new EFFilter();
//参数值集合
List<object> listArgs = new List<object>();
string filter = "1=1";
#region "处理动态过滤条件"
if (gridParam.FilterList != null && gridParam.FilterList.Count > 0)
{
StringBuilder sb = new StringBuilder();
int paramCount = 0;
DateTime dateTime;
//操作符
string strOperator = string.Empty;
foreach (var item in gridParam.FilterList)
{
//字段名称为空则跳过
if (string.IsNullOrEmpty(item.FieldName))
{
continue;
}
//匹配枚举,防止SQL注入
Operator operatorEnum = (Operator)Enum.Parse(typeof(Operator), item.Operator, true);
//跳过字段值为空的
if (operatorEnum != Operator.Null && operatorEnum != Operator.NotNull && string.IsNullOrEmpty(item.FieldValue))
{
continue;
}
strOperator = operatorEnum.GetDescription();
if (item.IgnoreCase && !item.IsDateTime)
{
//2016-07-19添加查询时忽略大小写比较
item.FieldValue = item.FieldValue.ToLower();
item.FieldName = string.Format("{0}.ToLower()", item.FieldName);
}
switch (operatorEnum)
{
//等于,不等于,小于,大于,小于等于,大于等于
case Operator.EQ:
case Operator.NE:
case Operator.GT:
case Operator.GE:
case Operator.LT:
case Operator.LE:
if (item.IsDateTime)
{
if (DateTime.TryParse(item.FieldValue, out dateTime))
{
if (!item.FieldValue.Contains("00:00:00") && dateTime.ToString("HH:mm:ss") == "00:00:00")
{
if (operatorEnum == Operator.LE)
{
listArgs.Add(DateTime.Parse(dateTime.ToString("yyyy-MM-dd") + " 23:59:59"));
}
else
{
listArgs.Add(dateTime);
}
}
else
{
listArgs.Add(dateTime);
}
sb.AppendFormat(" AND {0} {1} @{2}", item.FieldName, strOperator, paramCount);
}
}
else
{
listArgs.Add(ConvertToType(item.FieldValue, GetPropType<T>(item.FieldName)));
sb.AppendFormat(" AND {0} {1} @{2}", item.FieldName, strOperator, paramCount);
}
paramCount++;
break;
case Operator.Like:
case Operator.NotLike:
case Operator.LLike:
case Operator.RLike:
listArgs.Add(item.FieldValue);
if (operatorEnum == Operator.Like)
{
sb.AppendFormat(" AND {0}.Contains(@{1})", item.FieldName, paramCount);
}
else if (operatorEnum == Operator.NotLike)
{
sb.AppendFormat(" AND !{0}.Contains(@{1})", item.FieldName, paramCount);
}
else if (operatorEnum == Operator.LLike)
{
sb.AppendFormat(" AND {0}.EndsWith(@{1})", item.FieldName, paramCount);
}
else if (operatorEnum == Operator.RLike)
{
sb.AppendFormat(" AND {0}.StartsWith(@{1})", item.FieldName, paramCount);
}
paramCount++;
break;
case Operator.Null:
listArgs.Add(item.FieldValue);
sb.AppendFormat(" AND {0}=null", item.FieldName);
paramCount++;
break;
case Operator.NotNull:
listArgs.Add(item.FieldValue);
sb.AppendFormat(" AND {0}!=null", item.FieldName);
paramCount++;
break;
case Operator.In:
sb.AppendFormat(" AND (");
foreach (var schar in item.FieldValue.Split(','))
{
listArgs.Add(schar);
sb.AppendFormat("{0}=@{1} or ", item.FieldName, paramCount);
paramCount++;
}
sb.Remove(sb.Length - 3, 3);
sb.AppendFormat(" )");
break;
case Operator.NotIn:
sb.AppendFormat(" AND (");
foreach (var schar in item.FieldValue.Split(','))
{
listArgs.Add(schar);
sb.AppendFormat("{0}!=@{1} and ", item.FieldName, paramCount);
paramCount++;
}
sb.Remove(sb.Length - 3, 3);
sb.AppendFormat(" )");
break;
}
if (sb.ToString().Length > 0)
{
filter = sb.ToString().Substring(4, sb.Length - 4);
}
}
#endregion
}
result.Filter = filter;
result.ListArgs = listArgs;
return result;
}
Copy after login
总结
The above is the detailed content of Common sample code sharing for C#List to implement row to column conversion (picture). For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn