无法获取数据行中的数据

时间:2015-04-24 07:37:47

标签: c# dataset

这是我的主要测试用例方法,错误发生在

  

target.DeletePHIParmYield(rowToDelete);

[TestMethod()]
    public void DeletePHIParmYieldTest()
    {
        CommonPHIEditorBL_Accessor target = new CommonPHIEditorBL_Accessor();
        target.mCommonPHIEditorDS = new CommonPHIEditorDS();
        PHIEditorUniquePHIRowDefinition rowToDelete = new PHIEditorUniquePHIRowDefinition();
        //Test Case 1
        AddPHIYieldInfo(target.mCommonPHIEditorDS.PHIYieldParm, 1, 12, 123, 1234, 2001);
        rowToDelete.ItemID = "2001";
        rowToDelete.MetricKeyID = "12";
        rowToDelete.ProjectName = "Test";
        rowToDelete.ProjectType = "MOR";
        rowToDelete.Status = "MOR";
        rowToDelete.SubObject = "CFA";
        rowToDelete.Factory = "VF";
        rowToDelete.ParameterName = "BI%";
        target.DeletePHIParmYield(rowToDelete); //error inside here
        //Assert.AreEqual(0, target.mCommonPHIEditorDS.PHIYieldParm.Rows.Count);
        Assert.AreEqual(0, target.mCommonPHIEditorDS.PHIYieldParm.Rows.Count);

    }

进入DeletePHIParmYield(rowToDelete)函数:

    public void DeletePHIParmYield(PHIEditorUniquePHIRowDefinition rowToDelete)
        {
            int id;

            CommonPHIEditorDS.PHIYieldParmRow sourceRow = GetPHIRow(rowToDelete);
            if (sourceRow == null)
                throw new Exception("Row cannot be located!");

            if (!sourceRow.IsMetricModelInputIDNull())
                id = sourceRow.MetricModelInputID; 
            else
                id = sourceRow.InputID;
DeletePHIYieldParmDataAndAcceptChanges(sourceRow);
            sourceRow.Delete();

 if (id < 0 && sourceRow.RowState != DataRowState.Detached) //this record is not in DB. No need to call to DB
                sourceRow.AcceptChanges();
        }

转到CommonPHIEditorDS.PHIYieldParmRow sourceRow = GetPHIRow(rowToDelete);

public CommonPHIEditorDS.PHIYieldParmRow GetPHIRow(PHIEditorUniquePHIRowDefinition newRowObj)
    {
        CommonPHIEditorDS.PHIYieldParmRow row = null;

        string filter = ConstructFilterUsingPHIEditorUniquePHIRowDefinitionObject(newRowObj);

        CommonPHIEditorDS.PHIYieldParmRow[] existingRow
            = (CommonPHIEditorDS.PHIYieldParmRow[])MCommonPhiEditorDs.PHIYieldParm.Select(filter, string.Empty, DataViewRowState.CurrentRows);



        //var existingRow = from filter in MCommonPhiEditorDs.PHIYieldParm
        //                  where filter.MetricTypeName.StartsWith("BI")
        //                  select existingRow;
        //row = existingRow.FirstOrdefault();



        if (existingRow != null && existingRow.Length == 1)
            row = existingRow[0];

        return row;
    }

过滤器中的数据:(我确实改为“LIKE”而不是“=”来尝试)

  

MetricTypeName ='BI%'AND Factory ='VF'AND St​​atus ='MOR'AND ProjectName ='Test'AND ProjectType ='MOR'AND SubObjectName ='CFA'AND ItemID ='2001'AND MetricKeyID = '12 'AND MetricModelInputID IS NULL

PHIYieldParmData表包含以下列:(没有数据,我手动输入数据试试)

DataID, MetricModelInputID, MetricID, InputID, ItemID, DateKey, Value, HrzDisplay, HorizonID, MetricModelID, AggregateItemChar

通过该行

时无法获取数据
  

row = existingRow [0];

它无法通过上面的线路,但是马上跳到这里。因为if else条件。但是,我的 existingRow 存在问题,我无法将数据加载到

return row;

我的字符串包含数据列表,而 existingRow 为空

完整代码:

public CommonPHIEditorDS.PHIYieldParmRow GetPHIRow(PHIEditorUniquePHIRowDefinition newRowObj)
{
    CommonPHIEditorDS.PHIYieldParmRow row = null;

    string filter = ConstructFilterUsingPHIEditorUniquePHIRowDefinitionObject(newRowObj);

    CommonPHIEditorDS.PHIYieldParmRow[] existingRow
        = (CommonPHIEditorDS.PHIYieldParmRow[])MCommonPhiEditorDs.PHIYieldParm.Select(filter, string.Empty, DataViewRowState.CurrentRows);

    if (existingRow != null && existingRow.Length == 1)          
        row = existingRow[0];

    return row;
}

1 个答案:

答案 0 :(得分:0)

您评论说这是您DataTable.Select的过滤器:

MetricTypeName = 'BI%' AND 
Factory = 'VF' AND 
Status = 'MOR' AND 
ProjectName = 'Test' AND 
ProjectType = 'MOR' AND 
SubObjectName = 'CFA' AND 
ItemID = '2001' AND 
MetricKeyID = '1' AND 
MetricModelInputID IS NULL

您想要使用MetricTypeName LIKE 'BI%'吗?如果您使用的是=而不是LIKE,那么您正在搜索exaxt字符串而不是子字符串。

所以:

MetricTypeName LIKE 'BI%' AND 
// ....

但我更喜欢Linq-To-DataSet:

var rows = from phiRow in MCommonPhiEditorDs.PHIYieldParm
           where phiRow.MetricTypeName.StartsWith("BI")
            &&   phiRow.Factory == "VF"
           // ....
           select phiRow;
CommonPHIEditorDS.PHIYieldParmRow row = rows.FirstOrdefault();