我有一个列表:
[Table(Name = "ClassA")]
public class ClassA
{
public ClassA()
{
LinksToClassB = new EntitySet<ClassB>();
}
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY")]
public int ID { get; set; }
[Association]
public EntitySet<ClassB> LinksToClassB { get; set; } //=> 1 to n cardinality
[Association]
public ClassB OneLinkToClassB { get; set; }//=> 1 to 1 cardinality
}
[Table(Name = "ClassB")]
public class ClassB
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY")]
public int ID { get; set; }
[Column(CanBeNull = true)]
public string Name { get; set; }
}
public class DatabaseContext : DataContext
{
public Table<ClassA> ClassATable;
public Table<ClassB> ClassBTable;
public DatabaseContext(string connection) : base(connection) { }
}
[TestClass]
public class Test
{
string path = @"F:\Temp\Testspace - Forum Database\database.mdf";//path to database
[TestMethod]
public void TestMethod()
{
//creates Database
DatabaseContext context = new DatabaseContext(path);
if (context.DatabaseExists())//Delete if exists
{
context.DeleteDatabase();
}
context.CreateDatabase();
ClassB b1 = new ClassB(); b1.Name = "name 1";
ClassB b2 = new ClassB(); b2.Name = "name 2";
ClassB b3 = new ClassB(); b3.Name = "name 3";
ClassA a = new ClassA();
//now the references will be added to the object a
//in 1-n references
a.LinksToClassB.Add(b1);
a.LinksToClassB.Add(b2);
a.LinksToClassB.Add(b3);
//and the has-one reference (OneLinkToClassB)
a.OneLinkToClassB = b1;
context.ClassATable.InsertOnSubmit(a);
context.SubmitChanges(); //store in database
//now the database will be reloaded
context = new DatabaseContext(path);
//Check if all ClassB objects were correctly stored and reloaded
foreach (ClassB x in context.ClassBTable)
{
Console.WriteLine(x.ID + "; " + x.Name);
/*
-> expected output:
1; name 1
2; name 2
3; name 3
-> real output
1; name 1
2; name 2
3; name 3
-> check!
*/
}
//check if all ClassA objects were correctly stored and reloaded
foreach (ClassA x in context.ClassATable)//context.ClassATable has only one entry
{
Console.WriteLine("check of entitys set");
//check of 1-n references
foreach (ClassB b in x.LinksToClassB)
{
Console.WriteLine(x.ID + " has a link to " + b.ID + ", " + b.Name);
/*
-> expected output:
1 has a link to 1, name 1
1 has a link to 2, name 2
1 has a link to 3, name 3
-> real output
1 has a link to 1, name 1
-> doesn't match...
*/
}
Console.WriteLine("check of single link");
//check of 1-1 reference
Console.WriteLine(x.ID + " has a link to " + x.OneLinkToClassB.ID + ", " + x.OneLinkToClassB.Name);
/*
-> expected output:
1 has a link to 1, name 1
-> real output
this line throws an NullReferenceException
*/
}
}
}
我想做点什么
mylist = [{2: 1451989654}, {3: 1435222955}, {4: 1346726067}, {53: 1451887667}, {723: 1445763454}]
我最好能做到这一点吗?
答案 0 :(得分:5)
mylist
是一个列表,因此您需要首先遍历列表:
for dct in mylist:
for key, val in dct.items():
print key
print val
注意:您需要dict.items()
(或.iteritems()
)来获取密钥和值
答案 1 :(得分:1)
由于您的词典中没有重复的键,为什么要将它保存在列表中?
您可以将所有内容保存在一个词典中(或稍后再转换),这样就可以更轻松地访问数据(包括打印):
mylist = [{2: 1451989654}, {3: 1435222955}, {4: 1346726067}, {53: 1451887667}, {723: 1445763454}]
mydict = {k:d[k] for d in mylist for k in d} # Convert the list to one dictionary
for k in mydict:
print k, mydict[k]
并打印出来:
2 1451989654
3 1435222955
4 1346726067
53 1451887667
723 1445763454
但请注意,如果您想要订购数据,则可能需要默认为OrderedDict
并将转换替换为:
mydict = OrderedDict([(k, d[k]) for d in mylist for k in d])