C# ToDictionary 获取 C# 7 之前的匿名类型值
C# ToDictionary get Anonymous Type value before C # 7
你好,这是我在 C# 7 之后如何从字典 myage 值中获取值的方法
static void Main(string[] args)
{
List<User> userlist = new List<User>();
User a = new User();
a.name = "a";
a.surname = "asur";
a.age = 19;
User b = new User();
b.name = "b";
b.surname = "bsur";
b.age = 20;
userlist.Add(a);
userlist.Add(b);
var userlistdict = userlist.ToDictionary(x => x.name,x=> new {x.surname,x.age });
if(userlistdict.TryGetValue("b", out var myage)) //myage
Console.WriteLine(myage.age);
}
}
public class User {
public string name { get; set; }
public string surname { get; set; }
public int age { get; set; }
}
Okey结果is:20
但在 C# 7 之前,我如何从字典中获取 myage 值。我找不到任何其他 way.Just 我在 trygetvalue 方法中找到了 declare myage。
三个选项:
首先,你可以这样写一个扩展方法:
public static TValue GetValueOrDefault<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TKey key)
{
TValue value;
dictionary.TryGetValue(dictionary, out value);
return value;
}
然后将其命名为:
var result = userlist.GetValueOrDefault("b");
if (result != null)
{
...
}
其次,您可以通过提供虚拟值将 var
与 out
一起使用:
var value = new { surname = "", age = 20 };
if (userlist.TryGetValue("b", out value))
{
...
}
或根据评论:
var value = userlist.Values.FirstOrDefault();
if (userlist.TryGetValue("b", out value))
{
...
}
第三,你可以先用ContainsKey
:
if (userlist.ContainsKey("b"))
{
var result = userlist["b"];
...
}
另一种选择是将 User
对象存储为字典项值而不是匿名类型,然后您可以先声明类型并在 TryGetValue
.[=14= 中使用它]
var userlistdict = userlist.ToDictionary(x => x.name, x => x );
User user;
if (userlistdict.TryGetValue("b", out user))
{
Console.WriteLine(user.surname);
Console.WriteLine(user.age);
}
从列表创建词典的第一行与
相同
var userlistdict = userlist.ToDictionary(x => x.name);
你好,这是我在 C# 7 之后如何从字典 myage 值中获取值的方法
static void Main(string[] args)
{
List<User> userlist = new List<User>();
User a = new User();
a.name = "a";
a.surname = "asur";
a.age = 19;
User b = new User();
b.name = "b";
b.surname = "bsur";
b.age = 20;
userlist.Add(a);
userlist.Add(b);
var userlistdict = userlist.ToDictionary(x => x.name,x=> new {x.surname,x.age });
if(userlistdict.TryGetValue("b", out var myage)) //myage
Console.WriteLine(myage.age);
}
}
public class User {
public string name { get; set; }
public string surname { get; set; }
public int age { get; set; }
}
Okey结果is:20
但在 C# 7 之前,我如何从字典中获取 myage 值。我找不到任何其他 way.Just 我在 trygetvalue 方法中找到了 declare myage。
三个选项:
首先,你可以这样写一个扩展方法:
public static TValue GetValueOrDefault<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TKey key)
{
TValue value;
dictionary.TryGetValue(dictionary, out value);
return value;
}
然后将其命名为:
var result = userlist.GetValueOrDefault("b");
if (result != null)
{
...
}
其次,您可以通过提供虚拟值将 var
与 out
一起使用:
var value = new { surname = "", age = 20 };
if (userlist.TryGetValue("b", out value))
{
...
}
或根据评论:
var value = userlist.Values.FirstOrDefault();
if (userlist.TryGetValue("b", out value))
{
...
}
第三,你可以先用ContainsKey
:
if (userlist.ContainsKey("b"))
{
var result = userlist["b"];
...
}
另一种选择是将 User
对象存储为字典项值而不是匿名类型,然后您可以先声明类型并在 TryGetValue
.[=14= 中使用它]
var userlistdict = userlist.ToDictionary(x => x.name, x => x );
User user;
if (userlistdict.TryGetValue("b", out user))
{
Console.WriteLine(user.surname);
Console.WriteLine(user.age);
}
从列表创建词典的第一行与
相同 var userlistdict = userlist.ToDictionary(x => x.name);