LingQ to sql analysis

Linqpad 好似入唔到include咁

Context.post
       .Include(x => x.Children)
       .AsEnumerable()
       .Where(x => x.id == 5)
       .ToList();

我想知佢係load 所有post 先找Id 5.
還是點?
最好就是先找到id 5 post, 再找下邊children.

唔太熟linq .

Linqpad 好似入唔到include咁

Context.post
       .Include(x => x.Children)
       .AsEnumerable()
   ...
staymen 發表於 2020-12-25 23:10

= select Children from Post Where Id=5... EF Framework其實到尾都係gen一句sql出黎.. 一個linq statement出一個sql query..

TOP

回覆 2# 7h1r733n

咁寫會成個tree 拎黎出黎.        .AsEnumerable(), 但就唔知佢做左咩.
Context.post
       .Include(x => x.Children)
       .AsEnumerable()
       .Where(x => x.id == 5)
       .ToList();

TOP

你咁寫其實就係即係select+join哂所有野去client, 之後client做filtering
咁寫一多data爆硬

你del左個AsEnumerable就得

TOP

本帖最後由 7h1r733n 於 2021-1-3 05:17 編輯
回覆  7h1r733n

咁寫會成個tree 拎黎出黎.        .AsEnumerable(), 但就唔知佢做左咩.
Context.post
    ...
staymen 發表於 2020-12-31 12:42


https://stackoverflow.com/questi ... ata-from-the-server

其中一個reply 有講到呢樣嘢...

In fact, even the calls to IEnumerable<T>.Where() and IEnumerable<T>.Take() don't actually start fetching any rows at that moment. They simply setup wrapping IEnumerables that will filter results as they are iterated on. The fetching and iterating of the results really only begins when ToList() is called.

TOP

你咁寫其實就係即係select+join哂所有野去client, 之後client做filtering
咁寫一多data爆硬

你del左個AsEn ...
blue01299 發表於 2020-12-31 14:58



The AsEnumerable() operator, unlike ToList() and ToArray(), does not cause execution of the query. It is still deferred. The AsEnumerable() operator merely changes the static typing of the query, turning a IQueryable<T> (IQueryable (ofT) in Visual Basic) into an IEnumerable<T> (IEnumerable (ofT) in Visual Basic), tricking the compiler into treating the rest of the query as locally executed.

source: https://docs.microsoft.com/en-us ... redirectedfrom=MSDN

TOP

本帖最後由 7h1r733n 於 2021-1-4 17:33 編輯
回覆  7h1r733n

咁寫會成個tree 拎黎出黎.        .AsEnumerable(), 但就唔知佢做左咩.
Context.post
    ...
staymen 發表於 2020-12-31 12:42


帶返個頭盔先... 上面佢咁寫我咁睇....
不過一般黎講... 唔會咁寫..
通常都係context.enities.Where(x => x.Id == 1).ToList(); 就算..

TOP

回覆  7h1r733n

咁寫會成個tree 拎黎出黎.        .AsEnumerable(), 但就唔知佢做左咩.
Context.post
    ...
staymen 發表於 2020-12-31 12:42


Linq根基: extension method, 可以被overload 多個版本: IEnumerable<T>/IQueryable<T>/IOrderedQueryable<T>/etc.
呢個AsEnumerable只係確保後面Where嘅extension method係IEnumerable<T>.Where method, 副作用會斷開 linq SQL query, 導致實際執行ToList時向SQL Server取得全部資料(!)後再自己行IEnumerable<T>.Where逐個record去filter id=5嘅record入list...

正解就係移除 .AsEnumerable()個行令Where行其他overload版本(IQueryable<T>/IOrderedQueryable<T>/etc.)
p.s. .Include 嗰part...: https://stackoverflow.com/questi ... -include-do-in-linq

TOP

回覆 8# antlee

但係如果要成個TREE 拎, 如ID=5 以下TREE ,有無更好寫法?

TOP

回覆  antlee

但係如果要成個TREE 拎, 如ID=5 以下TREE ,有無更好寫法?
staymen 發表於 2021-1-5 16:17

https://entityframeworkcore.com/querying-data-joining

TOP