0

LinQ 一個迷惑

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.

var query = from garden in Plant
where garden.Name.bool().Contain(name.bool())
select garden;

foreach(garden in Plant)
{
}

from 後面是要寫 資料庫本身的資料表? 還是 List 所定義的name

Plant 是資料表 garden 是 List 所定義的 name

where garden.Name.ToLower().Contain(name.ToLower)))

where garden.Name.bool().Contain(name.bool())

Homura iT邦高手 1 級 ‧ 2018-10-31 09:29:56 檢舉
LINQ在編譯時就能抓到錯誤

ping1000 iT邦新手 5 級 ‧ 2018-10-31 16:02:51 檢舉

Homura iT邦高手 1 級 ‧ 2018-10-31 16:09:31 檢舉

2 個回答

0

iT邦大師 1 級 ‧ 2018-10-31 13:13:36

``````var query = from garden in Plant
where garden.Name.bool().Contain(name.bool())
select garden;
``````

``````var query = from garden in Plant
where garden.Name.bool()
select garden;
``````

where garden.Name.ToLower().Contain(name.ToLower)))

where garden.Name.bool().Contain(name.bool())

ping1000 iT邦新手 5 級 ‧ 2018-10-31 16:07:35 檢舉

var query = from plant in garden //from 後面的 plant 是要變數 還是 資料表?

where plant.Poisonous == true
select plant;

var query = from plant in garden //from 後面的 plant 是要變數 還是 資料表?

where plant.Poisonous == true
select plant;

ping1000 iT邦新手 5 級 ‧ 2018-10-31 16:38:32 檢舉

0
Homura
iT邦高手 1 級 ‧ 2018-10-31 14:19:29

from後面是你的變數名...

``````var query = from g in garden
where g.Name.Contain(name)
select g;
``````

ping1000 iT邦新手 5 級 ‧ 2018-10-31 16:00:42 檢舉

Homura iT邦高手 1 級 ‧ 2018-10-31 16:09:00 檢舉

ping1000 iT邦新手 5 級 ‧ 2018-10-31 16:42:35 檢舉

Homura iT邦高手 1 級 ‧ 2018-10-31 16:46:11 檢舉