Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将列表值映射到新对象属性_C#_.net_Dictionary_Enumerable - Fatal编程技术网

C# 如何将列表值映射到新对象属性

C# 如何将列表值映射到新对象属性,c#,.net,dictionary,enumerable,C#,.net,Dictionary,Enumerable,我有一本这样的字典 public Dictionary<string, List<string>> Options { get; set; } 所以底线是。。我想将Option1和Option2分配给字典中每个项目的相应值 像这样 如何正确执行此操作?一种方法是使用反射,如以下示例所示: public class ProductVariant { public ProductVariant() {

我有一本这样的字典

public Dictionary<string, List<string>> Options { get; set; }
所以底线是。。我想将
Option1
Option2
分配给字典中每个项目的相应值

像这样


如何正确执行此操作?

一种方法是使用反射,如以下示例所示:

public class ProductVariant
    {
        public ProductVariant()
        {
            
        }
        public string Key { get; set; }
        public string Option1 { get; set; }
        public string Option2 { get; set; }
        public string Option3 { get; set; }

        public static IEnumerable<ProductVariant> GetProductVariants(Dictionary<string, List<string>> options)
        {
            foreach (var optionList in options)
            {
                var pv = new ProductVariant();
                pv.Key = optionList.Key;

                var props = pv.GetType()
                    .GetProperties()
                    .Where(x=>x.CanWrite && x.CanWrite)
                    .Where(x=>x.Name!="Key")
                    .ToArray();

                for (int i = 0; i < props.Length; i++)
                {
                    props[i].SetValue(pv,optionList.Value[i]);
                }

                yield return pv;
            }
        }
    }
公共类ProductVariant
{
公共产品变量()
{
}
公共字符串密钥{get;set;}
公共字符串选项1{get;set;}
公共字符串选项2{get;set;}
公共字符串选项3{get;set;}
公共静态IEnumerable GetProductVariants(字典选项)
{
foreach(期权中的var期权列表)
{
var pv=新产品变量();
pv.Key=optionList.Key;
var props=pv.GetType()
.GetProperties()
.Where(x=>x.CanWrite&&x.CanWrite)
.Where(x=>x.Name!=“Key”)
.ToArray();
for(int i=0;i
我测试了这个方法,并且正在工作。您仍然需要进行一些验证以避免错误


您可以在此处看到该方法:

您的ProductVariant必须有一个包含字典的属性。然后创建另外3个属性,get将在其中给出此词典的正确索引结果。var test=newproductvariant(yourDictionnary)test.Option1 test.Option2 test.Option3公共字符串Option1=>\u myDictionnary[0];public Option2=>_my….[1]所以它不能,因为它是第三方库。所以,
Options
中的每个键都应该有一个对应的
ProductVariant
项,其值来自字典值?是的,就像我试图用图片描述它一样,我尽了最大努力哈哈
public class ProductVariant
    {
        public ProductVariant()
        {
            
        }
        public string Key { get; set; }
        public string Option1 { get; set; }
        public string Option2 { get; set; }
        public string Option3 { get; set; }

        public static IEnumerable<ProductVariant> GetProductVariants(Dictionary<string, List<string>> options)
        {
            foreach (var optionList in options)
            {
                var pv = new ProductVariant();
                pv.Key = optionList.Key;

                var props = pv.GetType()
                    .GetProperties()
                    .Where(x=>x.CanWrite && x.CanWrite)
                    .Where(x=>x.Name!="Key")
                    .ToArray();

                for (int i = 0; i < props.Length; i++)
                {
                    props[i].SetValue(pv,optionList.Value[i]);
                }

                yield return pv;
            }
        }
    }