iT邦幫忙

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.

我要在這 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())

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 檢舉
你這該不會是考試吧@@
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

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;

已經使用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())

照上面邏輯,都不應該

看更多先前的回應...收起先前的回應...
ping1000 iT邦新手 5 級 ‧ 2018-10-31 16:07:35 檢舉

依照樓下那位大大所說的~ 我要搜尋的是 garden 這一個 List
所以我應該要改成

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

然後題目是說 編寫一個選擇的LINQ表達式那些有毒的植物。 Poisonous 是 bool
所以我應該改成
where plant.Poisonous == true
select plant;

這樣對嗎?

暐翰 iT邦大師 1 級 ‧ 2018-10-31 16:12:15 檢舉

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

變數

然後題目是說 編寫一個選擇的LINQ表達式那些有毒的植物。 Poisonous 是 bool
所以我應該改成
where plant.Poisonous == true
select plant;
這樣對嗎?

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

恩恩~那我明了了~感恩>Q<

暐翰 iT邦大師 1 級 ‧ 2018-10-31 16:39:44 檢舉

/images/emoticon/emoticon12.gif

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

我只說第一個錯誤
你要搜尋的對象是garden這個List
你的語法完全錯誤
對象要放在in後面
from後面是你的變數名...
所以要改成

var query = from g in garden
where g.Name.Contain(name)
select g;
看更多先前的回應...收起先前的回應...
ping1000 iT邦新手 5 級 ‧ 2018-10-31 16:00:42 檢舉

所以 from 後面不是接 SQL 的資料表? 而是一個想要的變數?

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

恩恩

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

我一直以為 from 後面是接 資料表@@

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

我要發表回答

立即登入回答