1

Hi I have problem in reading a data table which has given id in the dictionary. Any one help me please. Below is my code. I have a dictionary datasourceData I am storing data table. I need to retrieve the datatable stored in the dictionary for the given id.

public Dictionary<Guid, DataTable> dataSourceData { get; set; }

public DataModel()
        {
            dataSourceData = new Dictionary<Guid, DataTable>();
            logger = new BasicFileLogger.Logger();
        }

public DataTable GetDataSourceDescriptionById(Guid piId)
        {
            if (piId == null || piId == Guid.Empty)
                throw new InvalidOperationException("No such datasourcedescriptionId");
            try
            {
                return dataSourceData.Values.Where(db => db.Equals(piId));
            }
            catch (Exception ex)
            {
                logger.WriteException(ex);
                throw new InvalidOperationException(ex.Message);
            }
        }

Help please?

4

2 に答える 2

2

この行を変更したい

return dataSourceData.Values.Where(db => db.Equals(piId));

return dataSourceData[piId];
于 2012-08-24T10:07:58.827 に答える
1

キーを使用して簡単にアクセスできます。

if(dataSourceData.ContainsKey(yourGUID))
    DataTable dt = dataSourceData[yourGUID];

アクセスしたい場所yourGUIDは、辞書にキーが含まれているかどうかを確認できるため、メソッドは次のようになります。GUIDGUID

public DataTable GetDataSourceDescriptionById(Guid piId)
        {
            Dictionary<Guid, DataTable> dataSourceData = new Dictionary<Guid, DataTable>();
            if (dataSourceData.ContainsKey(piId))
            {
                return dataSourceData[piId];
            }
            else
            {
                InvalidOperationException ex = new InvalidOperationException("No such datasourcedescriptionId"));
                logger.WriteException(ex);
                throw ex;
            }
        }
于 2012-08-24T10:08:46.967 に答える