Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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# 比较linq查询中的数组元素和字符串_C#_Linq - Fatal编程技术网

C# 比较linq查询中的数组元素和字符串

C# 比较linq查询中的数组元素和字符串,c#,linq,C#,Linq,我有这个模型: public class Model_Test { public string Prop1 { get; set; } public string Prop2 { get; set; } public string[] Prop3 { get; set; } } 检查Prop3是否为数组 Prop3中的值类似于{“answer1”、“answer2”、“answer3”} 我需要做一个只接

我有这个模型:

 public class Model_Test
    {
        public string Prop1 { get; set; }            
        public string Prop2 { get; set; }
        public string[] Prop3 { get; set; }
    }
检查
Prop3
是否为数组

Prop3
中的值类似于
{“answer1”、“answer2”、“answer3”}

我需要做一个只接受
Model\u Test
对象的查询,其中
answer3
为“是”,我正在尝试:

result = from q in Model_Test
         where q.Prop3[2] == "Yes"                    
         select new { Name = Prop1, Value = Prop2 };
执行此查询时,会出现以下错误:

无法识别的表达式节点:ArrayIndex

我想问题出在我查询的这一部分:
q.Prop3[2]


感谢您的帮助。

问题在于表达式解析器不知道如何将数组索引操作转换为等效的SQL。
您可以使用任何贪婪运算符(例如ToArray())获取所有数据,然后在内存中对其进行筛选。

您正在寻找基本linq
,其中
查询:

// Your Model
public class Model_Test
    {
        public string Prop1 { get; set; }            
        public string Prop2 { get; set; }
        public bool[] Prop3 { get; set; }
    }

//Usage
List<Model_Test> lst = new List<Model_Test>(){
        new Model_Test{Prop1="Foo", Prop2="Bar",Prop3 = new bool[]{true,false,true}},
        new Model_Test{Prop1="Foo2", Prop2="Bar2",Prop3 = new bool[]{true,false,false}},
        new Model_Test{Prop1="Foo3", Prop2="Bar3",Prop3 = new bool[]{true,false,true}},
    };

    // Query Expression
    var result = from element in lst
                where element.Prop3[2] == true
                select new {Name = element.Prop1, Value = element.Prop2};

    // Lambda Expression    
    var result2 = lst.Where(x=>x.Prop3[2]==true).Select(x=> new {Name=x.Prop1, Value = x.Prop2});
//您的模型
公共类模型检验
{
公共字符串Prop1{get;set;}
公共字符串Prop2{get;set;}
公共bool[]Prop3{get;set;}
}
//用法
List lst=新列表(){
新的模型测试{Prop1=“Foo”,Prop2=“Bar”,Prop3=newbool[]{true,false,true},
新的模型测试{Prop1=“Foo2”,Prop2=“Bar2”,Prop3=newbool[]{true,false,false},
新的模型测试{Prop1=“Foo3”,Prop2=“Bar3”,Prop3=newbool[]{true,false,true},
};
//查询表达式
var结果=来自lst中的元素
其中元素.Prop3[2]==true
选择新的{Name=element.Prop1,Value=element.Prop2};
//Lambda表达式
var result2=lst.Where(x=>x.Prop3[2]==true)。选择(x=>new{Name=x.Prop1,Value=x.Prop2});

但是您需要确保在
Prop3[2]
处会有一个值,否则会引发异常。

Model\u Test
是查询代码块中的集合吗?是的Model\u Test是一个集合,这个:
Name=Prop1,value=Prop2
应该是:
Name=`q.Prop1,Value=q.Prop2
它是sql的linq或实体的iinq
Model\u Test
如何成为一个集合和一个类的名称。。。。??