2

I want to store objects in a List and have therefore created a class with 3 properties; prop1. prop2 and prop3.

To add a new Item to my List I'm using for example

_st.Add(new Bld(100,20,"Test1");
_st.Add(new Bld(101,20,"Test2");
_st.Add(new Bld(102,54,"Test3");

Now: How can I search the list to get the index of an Item that contains myObj.prop2 = 20 AND myObj.prop3="Test1"?

I've tried something like

_st.Where(tk => tk.prop1 == 1000 AND tk.Title == "Test1");

but this obviously doesn't work.

Any idea how to solve this problem?

Thanks in advance

4

4 回答 4

4

您应该使用FindIndex方法,并使用&&而不是AND

_st.FindIndex(tk => tk.prop1 == 1000 && tk.Title == "Test1");
于 2013-01-05T11:56:55.323 回答
2

要获取项目的索引,请使用FindIndex方法,例如

_st.FindIndex(tk => tk.prop1 == 1000 && tk.Title == "Test1")

希望这会有所帮助!

于 2013-01-05T12:02:11.337 回答
1

SQL 运算符不需要应用:

_st.Where(tk => tk.prop1 == 1000 && tk.Title == "Test1");
于 2013-01-05T11:58:52.297 回答
0

你必须试试这个:FindIndex 方法

int index = _st.FindIndex(tk => tk.prop1 == 1000 && tk.Title == "Test1");

或者

int index1 = _st.Select((tk, index) => new { _st = tk, Index = index })
          .FirstOrDefault(tk => tk._st.prop1 == 1000 && tk._st.Name_ar == "Test1")
          .Index;
于 2013-01-05T11:59:06.083 回答