datatable.select distinct

ContractedBlock.gif ExpandedBlockStart.gif Code
 /// <summary>
    
/// 返回执行Select distinct后的DataTable
    
/// </summary>
    
/// <param name="SourceTable">源数据表</param>
    
/// <param name="FieldNames">字段集</param>
    
/// <returns></returns>
    private DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames)
    {
        
object[] lastValues;
        DataTable newTable;
        DataRow[] orderedRows;

        
if (FieldNames == null || FieldNames.Length == 0)
            
throw new ArgumentNullException("FieldNames");

        lastValues 
= new object[FieldNames.Length];
        newTable 
= new DataTable();

        
foreach (string fieldName in FieldNames)
            newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);

        orderedRows 
= SourceTable.Select(""string.Join(",", FieldNames));

        
foreach (DataRow row in orderedRows)
        {
            
if (!fieldValuesAreEqual(lastValues, row, FieldNames))
            {
                newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));

                setLastValues(lastValues, row, FieldNames);
            }
        }

        
return newTable;
    }

    
private bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
    {
        
bool areEqual = true;

        
for (int i = 0; i < fieldNames.Length; i++)
        {
            
if (lastValues[i] == null || !lastValues[i].Equals(currentRow[fieldNames[i]]))
            {
                areEqual 
= false;
                
break;
            }
        }

        
return areEqual;
    }

    
private DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
    {
        
foreach (string field in fieldNames)
            newRow[field] 
= sourceRow[field];

        
return newRow;
    }

    
private void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
    {
        
for (int i = 0; i < fieldNames.Length; i++)
            lastValues[i] 
= sourceRow[fieldNames[i]];
    }

 

 

datatable.select

 

    public DataTable FillTable(string M_where, DataTable m_table)
    {
        DataTable _temp = m_table.Clone();
        DataRow[] _row = m_table.Select(M_where);

        foreach (DataRow _r in _row)
        {

            _temp.Rows.Add(_r.ItemArray);
        }
        return _temp;
    }

 

ViewState["BackUrl"] = Request.UrlReferrer.ToString();

 

    protected void Button1_Click1(object sender, EventArgs e)
    {
        if (ViewState["BackUrl"] != null)
        {
            Response.Redirect(ViewState["BackUrl"].ToString());
        }
    }

转载于:https://www.cnblogs.com/legend_sun/archive/2009/03/11/1408934.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值