In parts (a)–(c) below suppose there exists a class called Plant that represents the
plants found in a typical garden. Each plant has three public properties: Name (a
string), Planted (the DateTime the plant was placed in the ground) and Poisonous
(a bool, whether or not a person would become sick if the plant was eaten).
a. Assuming the following declaration of a collection of Plants
List garden = new List();
that has been filled with Plant objects, write a LINQ expression that selects
those plants that are poisonous.
我要在這 List garden = new List(); 底下 使用 LinQ 語法
我目前是寫
var query = from garden in Plant
where garden.Name.bool().Contain(name.bool())
select garden;
foreach(garden in Plant)
{
view.Add(garden);
}
這樣是對的嗎?
我目前迷惑的是 from garden in Plant 這一段
from 後面是要寫 資料庫本身的資料表? 還是 List 所定義的name
Plant 是資料表 garden 是 List 所定義的 name
還有 where 那邊我應該要寫
where garden.Name.ToLower().Contain(name.ToLower)))
還是
where garden.Name.bool().Contain(name.bool())
我目前是寫
var query = from garden in Plant where garden.Name.bool().Contain(name.bool()) select garden;
已經使用bool
做完植物名稱是否有毒判斷,為何還要在contain一次
這段應該改成:
var query = from garden in Plant
where garden.Name.bool()
select garden;
還有 where 那邊我應該要寫
where garden.Name.ToLower().Contain(name.ToLower)))
還是
where garden.Name.bool().Contain(name.bool())
照上面邏輯,都不應該
依照樓下那位大大所說的~ 我要搜尋的是 garden 這一個 List
所以我應該要改成
var query = from plant in garden //from 後面的 plant 是要變數 還是 資料表?
然後題目是說 編寫一個選擇的LINQ表達式那些有毒的植物。 Poisonous 是 bool
所以我應該改成
where plant.Poisonous == true
select plant;
這樣對嗎?
我只說第一個錯誤
你要搜尋的對象是garden
這個List
你的語法完全錯誤
對象要放在in
後面
from後面是你的變數名...
所以要改成
var query = from g in garden
where g.Name.Contain(name)
select g;