查找更新列表中的自定义对象 [英] Finding an updating a custom object in a list

查看:69
本文介绍了查找更新列表中的自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序遍历数据库中的表。对于每个具有主键的表,它需要知道主键列。



我正在使用OleDb的GetOleDbSchemaTable,它返回一个像这样的DataTable: />

 Primay_Key_Name,Table_Name,Column_Name,Ordinal 
PK_ZipCode,Cities,ZipCode,1
PK_ZipCode,Cities,ZipPlus4,2
PK_ProjectId,Projects,ID 1
PK_UsersId,用户,ID 1

该示例显示3个主键。有4条记录,因为PK_ZipCode是一个复合键,有两列,ZipCode和ZipPlus4。



我想将它存储在一个列表中,这样我就不必在以后循环访问数据库来获取列。



我应该使用哪种类型的清单?我在C#Collections上观看了一个8小时的PluralSight视频,但却发现强类型集合现在已经遗留了,我应该使用Generic Collections。



为了避免观看另一个视频,我创建了一个存储数据的对象。我将它存储在List< primarykeyobj>中。

公共类PrimaryKeyObj 
{
private string schema = null;
private List< string> columns = new List< string> {};
private string table = null;

公共字符串架构{get =>模式; set => schema = value; }
public List< string>列{get =>列; set => columns = value; }
公共字符串表{get =>表; set => table = value; }

public PrimaryKeyObj(字符串模式,字符串表,List< string> columns)
{
Schema = schema;
表=表;
Columns = columns;
}
}





我目前正在进行2次查询:



(1)返回一个Primay Keys列表并创建我的自定义对象

(2)使用Primary Key的列列表填充我的对象。< br $>


步骤1:生成PrimaryKeyObj自定义对象列表:

 dt = dbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, 
new Object [] {null,null,null});

dv = dt.DefaultView;
dv.RowFilter =TABLE_NAME不喜欢'MSys%'和ORDINAL = 1;
dt = dv.ToTable();

var primarykeys = new List< PrimaryKeyObj> {};
var primarykeycolumns = new List< string> {};

for(int i = 0; i< dt.Rows.Count; i ++)
{
string schema = dt.Rows [i] [TABLE_SCHEMA]。 ToString();
string table = dt.Rows [i] [TABLE_NAME] .ToString();
PrimaryKeyObj pko = new PrimaryKeyObj(schema,table,primarykeycolumns);
primarykeys.Add(pko);
}

下一部分是我被困的地方。现在我有主键列表,我需要用主键列名填充每一个。



步骤2:循环主键,找到我的列表中的键< primaykeyobj>并使用列名更新该键。

 dt = dbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys,
new Object [] {null,null,null});
dv = dt.DefaultView;
dv.Sort =TABLE_SCHEMA,TABLE_NAME,PK_NAME,ORDINAL asc;
dv.RowFilter =TABLE_NAME不喜欢'MSys%';
dt = dv.ToTable();

for(int i = 0; i< dt.Rows.Count; i ++)
{
string schema = dt.Rows [i] [TABLE_SCHEMA]。 ToString();
string table = dt.Rows [i] [TABLE_NAME] .ToString();
string column = dt.Rows [i] [COLUMN_NAME] .ToString();

PrimaryKeyObj PkObj = {做一些事情来返回具有此架构和表格的PrimaryKeyObj}
pko.Columns.Add(column);
}





我的尝试:



我尝试将其写为字典,并作为List< string>保持架构,List< string>拿着桌子然后卡在如何将柱子放入折叠中。我已经尝试了很多其他的东西,但它是在凌晨4点之后,所以请问我是否需要更多信息。

解决方案

我不确定我理解你正确的,但是......



如果你想得到关于主键信息的表及其列的列表,你必须改变你的 PrimaryKeysObj 这样:

  public   class  PrimaryKeyObj 
{
private string schema = < span class =code-keyword> string .Empty;
private string sPK = string .Empty;
private 列表< string> columns = new List< string>();
private string table = string .Empty;

public string 架构{获取 = > 架构; set = > schema = value ; }
public List< string>列{获取 = > 列; set = > columns = value ; }
public string 表{ get = > 表; set = > table = value ; }
public string PrimaryKey { get = > sPK; set = > sPK = value ; }

public PrimaryKeyObj( string schema,字符串表,字符串 sPrimaryKey,List< string> columns)
{
Schema = schema;
表=表;
PrimaryKey = sPrimaryKey;
Columns = columns;
}
}





这可以帮助您存储所有信息。

例如:

 List< PrimaryKeyObj> primarykeys = 
dt.AsEnumerable()
.GroupBy(x => x.Field< string>( TABLE_NAME))
。选择(grp = > new PrimaryKeyObj

grp.Select(a => a.Field< string>( TABLE_SCHEMA))。First(),
grp.Key,
grp.Select(a => a.Field< string>( PRIMARY_KEY_NAME))。First(),
grp.Select(c = > c.Field< string>( COLUMN_NAME))。ToList()
))。ToList();





现在,你的 primarykeys 应该存储 PrimaryKeyObj


My application iterates through the tables in my database. For each table that has a Primary Key, it needs to know the Primary Key columns.

I'm using OleDb's GetOleDbSchemaTable, which returns a DataTable like this:

Primay_Key_Name, Table_Name, Column_Name, Ordinal
PK_ZipCode,      Cities,     ZipCode,     1
PK_ZipCode,      Cities,     ZipPlus4,    2
PK_ProjectId,    Projects,   ID           1
PK_UsersId,      Users,      ID           1

The example shows 3 Primary Keys. There are 4 records because PK_ZipCode is a composite key with two columns, ZipCode and ZipPlus4.

I want to store this in a list, so that I don't have to hit the database later, when I loop through the tables to get the columns.

What type of list should I use? I watched an 8 hour PluralSight video on C# Collections, only to discover that strongly typed collections are now "legacy" and that I should be using Generic Collections instead.

To avoid watching another video, I've created an object to store the data. I'll store it in a List<primarykeyobj>.

public class PrimaryKeyObj
{
    private string schema = null;
    private List<string> columns = new List<string> { };
    private string table = null;

    public string Schema { get => schema; set => schema = value; }
    public List<string> Columns { get => columns; set => columns = value; }
    public string Table { get => table; set => table = value; }

    public PrimaryKeyObj (string schema, string table, List<string> columns )
    {
        Schema = schema;
        Table = table;
        Columns = columns;
    }
}



I'm currently doing it in 2 queries:

(1) returns a list of Primay Keys and creates my custom objects
(2) populates my object with the Primary Key's list of columns.

Step 1: Generate the list of PrimaryKeyObj custom objects:

dt = dbConnection.GetOleDbSchemaTable ( OleDbSchemaGuid.Primary_Keys ,
            new Object[ ] { null , null, null } );

dv = dt.DefaultView;
dv.RowFilter = "TABLE_NAME NOT LIKE 'MSys%' and ORDINAL = 1";
dt = dv.ToTable ( );

var primarykeys = new List<PrimaryKeyObj> { };
var primarykeycolumns = new List<string> { };

for ( int i = 0 ; i < dt.Rows.Count ; i++ )
{
    string schema = dt.Rows[ i ][ "TABLE_SCHEMA" ].ToString ( );
    string table = dt.Rows[ i ][ "TABLE_NAME" ].ToString ( );
    PrimaryKeyObj pko = new PrimaryKeyObj ( schema , table, primarykeycolumns );
    primarykeys.Add ( pko );
}

This next part is where I'm stuck. Now that I have the list of Primary Keys, I need to populate each one with the primary key column names.

Step 2: Loop through the Primary Keys, locate the key in my List<primaykeyobj> and update that key with the column names.

dt = dbConnection.GetOleDbSchemaTable ( OleDbSchemaGuid.Primary_Keys ,
			new Object[ ] { null , null , null } );                                        
dv = dt.DefaultView;
dv.Sort = "TABLE_SCHEMA, TABLE_NAME, PK_NAME, ORDINAL asc";
dv.RowFilter = "TABLE_NAME NOT LIKE 'MSys%'";                                                                                          
dt = dv.ToTable ( );

for ( int i = 0 ; i < dt.Rows.Count ; i++ )
{
	string schema = dt.Rows[ i ][ "TABLE_SCHEMA" ].ToString ( );
	string table = dt.Rows[ i ][ "TABLE_NAME" ].ToString ( );
	string column = dt.Rows[ i ][ "COLUMN_NAME" ].ToString ( );

	PrimaryKeyObj PkObj = {Do something to return the PrimaryKeyObj that has this schema and table} 
	pko.Columns.Add ( column );						
}



What I have tried:

I'm tried writing this as a Dictionary, and as a List<string> to hold the schema, a List<string> to hold the table but then got stuck on how to bring the columns into the fold. I've tried many other things, but it's after 4am, so please just ask me if you need any more information.

解决方案

I'm not sure i understand you correctly, but...

If you want to get list of tables and their columns with information about primary keys, you have to change your PrimaryKeysObj this way:

public class PrimaryKeyObj
{
 	private string schema = string.Empty;
	private string sPK = string.Empty;
    private List<string> columns = new List<string>();
    private string table = string.Empty;

    public string Schema { get => schema; set => schema = value; }
    public List<string> Columns { get => columns; set => columns = value; }
    public string Table { get => table; set => table = value; }
	public string PrimaryKey { get => sPK; set => sPK = value; }

    public PrimaryKeyObj (string schema, string table, string sPrimaryKey, List<string> columns )
    {
        Schema = schema;
        Table = table;
		PrimaryKey = sPrimaryKey;
        Columns = columns;
    }
}



This should help you to store entire information.
For example:

List<PrimaryKeyObj> primarykeys =
    dt.AsEnumerable()
    .GroupBy(x=>x.Field<string>("TABLE_NAME"))
    .Select(grp => new PrimaryKeyObj
    (
        grp.Select(a=>a.Field<string>("TABLE_SCHEMA")).First(),
        grp.Key,
        grp.Select(a=>a.Field<string>("PRIMARY_KEY_NAME")).First(),
        grp.Select(c=> c.Field<string>("COLUMN_NAME")).ToList()
    )).ToList();



Now, your primarykeys should store the list of PrimaryKeyObj.


这篇关于查找更新列表中的自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆