C# 使用整数进行字典查找,类型为。键和值类型的通用访问和设置

C# 使用整数进行字典查找,类型为。键和值类型的通用访问和设置,c#,.net,dictionary,csla,C#,.net,Dictionary,Csla,NET是我的公司在我们大多数项目中大量使用的一个框架,因此其中一些约束是框架本身强加的,无法更改。我会尽力指出这些限制 我有一组大约50个类,它们基本上是一个字典(包装在CSLA类型中),提供了一个查找的单例实例,可在程序中的各个地方使用,这一事实都很相似 这些类的结构大致如下 public class SomeLookup : Csla.NameValueListBase<Integer, SomeLookupPair> { private static SomeLooku

NET是我的公司在我们大多数项目中大量使用的一个框架,因此其中一些约束是框架本身强加的,无法更改。我会尽力指出这些限制

我有一组大约50个类,它们基本上是一个字典(包装在CSLA类型中),提供了一个查找的单例实例,可在程序中的各个地方使用,这一事实都很相似

这些类的结构大致如下

public class SomeLookup : Csla.NameValueListBase<Integer, SomeLookupPair>
{
    private static SomeLookup _list
    private SomeLookup
    {
        //DataPortal.Fetch Calls the DataPortal_Fetch and returns the <T>
        if (_list != null) { _list = DataPortal.Fetch<SomeLookup>; }
    }

    public static SomeLookup GetSomeLookup(Object criteria)
    {
        return new SomeLookup;
    } 

    public override void DataPortal_Fetch(Object criteria)
    {
        using(SqlClient.SqlConnection cn = new SqlClient.SqlConnection(ConnectionString))
        {
            cn.Open();
            using(SqlClient.SqlCommand = new SqlClient.SqlCommand)
            {
                cm.Connection = cn;
                cm.CommandType = CommandType.StoredProcedure;
                cm.CommandText = "getSomeLookup"

                using(var dr = new Csla.Data.SafeDataReader(cm.ExecuteReader)
                {
                    while(dr.Read)
                    {
                        //populates the interal nameValueList with lookup key/value pairs
                        Add(new NameValuePair(dr.GetInt32("Id"), 
                                              new SomeLookupPair { Code = dr.GetString("code"), 
                                                                   Description = dr.GetString("Description") });
                    }
                }
            }
        }
    }
}

public class SomeLookupPair
{
   public string Code {get; set;}
   public string Description {getl set;}
}
private integer _someLookupID  { get { //retrived from database and stored }; set { _someLookupId = value; }

public SomeLookupPair _someLookupPair { get { (SomeLookUp.GetSomeLookup)[_someLookupID] }; }

public void setSomeLookupID(SomeLookupPair pair)
{
   _someLookupId = (SomeLookup.GetSomeLookUp).Where(s => s.Value(pair)).Select(s => s.Key).SingleOrDefault
}
因此,引用此查找中的值的对象将在数据库中建模,只存储ID字段

Table SomeObject
   ID int PK
   SomeLookupId int FK
   .....
但在类中,我只想显示描述或代码(用户描述、内部/业务使用代码)

我的问题是,在处理这种情况时,我的类需要如下访问对象

public class SomeLookup : Csla.NameValueListBase<Integer, SomeLookupPair>
{
    private static SomeLookup _list
    private SomeLookup
    {
        //DataPortal.Fetch Calls the DataPortal_Fetch and returns the <T>
        if (_list != null) { _list = DataPortal.Fetch<SomeLookup>; }
    }

    public static SomeLookup GetSomeLookup(Object criteria)
    {
        return new SomeLookup;
    } 

    public override void DataPortal_Fetch(Object criteria)
    {
        using(SqlClient.SqlConnection cn = new SqlClient.SqlConnection(ConnectionString))
        {
            cn.Open();
            using(SqlClient.SqlCommand = new SqlClient.SqlCommand)
            {
                cm.Connection = cn;
                cm.CommandType = CommandType.StoredProcedure;
                cm.CommandText = "getSomeLookup"

                using(var dr = new Csla.Data.SafeDataReader(cm.ExecuteReader)
                {
                    while(dr.Read)
                    {
                        //populates the interal nameValueList with lookup key/value pairs
                        Add(new NameValuePair(dr.GetInt32("Id"), 
                                              new SomeLookupPair { Code = dr.GetString("code"), 
                                                                   Description = dr.GetString("Description") });
                    }
                }
            }
        }
    }
}

public class SomeLookupPair
{
   public string Code {get; set;}
   public string Description {getl set;}
}
private integer _someLookupID  { get { //retrived from database and stored }; set { _someLookupId = value; }

public SomeLookupPair _someLookupPair { get { (SomeLookUp.GetSomeLookup)[_someLookupID] }; }

public void setSomeLookupID(SomeLookupPair pair)
{
   _someLookupId = (SomeLookup.GetSomeLookUp).Where(s => s.Value(pair)).Select(s => s.Key).SingleOrDefault
}

感觉有更好的方法来处理
SomeLookupID
的值设置,我可以直接进行设置

据我所知,您应该将is作为单个属性编写:

public SomeLookupPair SomeLookupPair 
{ 
   get 
   { 
      (SomeLookUp.GetSomeLookup)[_someLookupID] }; 
   }
   set
   {
     _someLookupId = (SomeLookup.GetSomeLookUp).Where(s => s.Value(value)).Select(s => s.Key).SingleOrDefault;
   }
}
我想获得更高的性能(实际上现在你循环了所有的值),你可以重构SomeLookupPair并包含ID(我认为它是为你的查找而设计的,因为现在你不使用对键的性能访问!!!)。 这样,您就可以直接在setter中访问选定的id

public SomeLookupPair SomeLookupPair 
{ 
   get 
   { 
      (SomeLookUp.GetSomeLookup)[_someLookupID] }; 
   }
   set
   {
     if(value == null) throw new ArgumentNullException();
     _someLookupId = value.ID;
   }
}

您是否可以创建一个字典,并以这种方式获取NameValuePair/Key..@DJKRAZE该对类型的原因是实际上有一种干净的方式来访问需要使用的对象。e、 g.
SomeLookupPair.code
vs
SomeLookupPair[0]
更强的OO和类型引用Gi认为您可以在foreach(someLookup中的KeyValuePair对)的行中做更多的事情,但因为您想做更多的OO方法忽略不计。Sorry仅查看您的代码,您似乎正在使用SingleOrDefault在Lamba中处理它,如果它找不到一个或找到第一个实例