我试图查询嵌套链表,其中结果是子值的乘积乘以父列表中的值,然后添加结果。
此代码有效:
var X = (from B in Main.Globals.BookLL
from G in B.GreeksLL
where B.DealNo == 1 && B.Strategy == "Condor" &&
G.BookOrComp == "Book" && G.GreekType == "Delta"
select new
{
Total = G.Data[3] * B.BookPosn * B.FxRate
}).Sum(s=>s.Total);
...但我更喜欢使用lambda。下面的代码给出了编译错误,显示为该行末尾的注释。
double Z = Globals.BookLL.Where(B => B.DealNo == 1 && B.Strategy == "Condor").
SelectMany(G => G.GreeksLL).
Where(G => G.BookOrComp == "Book" && G.GreekType == "Delta").
Select(G => new { Total = G.Data[3] * B.BookPosn*B.FxRate }). // Compile error "B does not exist in the current context"
Sum();
我不知道如何操作,请查看并更正查询?感谢。
答案 0 :(得分:1)
尝试:
double Z = Globals.BookLL.Where(B => B.DealNo == 1 && B.Strategy == "Condor").
SelectMany(par => par.GreeksLL, (parent, child) => new { G = child, B = parent }).
Where(both => both.G.BookOrComp == "Book" && both.G.GreekType == "Delta").
Select(both => new { Total = both.G.Data[3] * both.B.BookPosn*both.B.FxRate }).
Sum(x => x.Total);
我的命名有点奇怪,但我希望你明白这一点,基本上当你做SelectMany()
时你'放弃了'B,这应该就是这样。
未经测试,请告诉我它是否有效。
使用结果选择器功能,查看SelectMany()
$this->db->select('title'); // your column
$this->db->from('table'); // your table
$result = $this->db->get()->result(); // get result
$title = $result->first_row()->title; // get ist row using first_row with your field name
$graph->title->Set($title); // as you are using for graph
。
答案 1 :(得分:1)
另一种方法是使用this重载过滤SelectMany中的GreekLL,然后使用Sum扩展名:
double z = Main.Globals.BookLL.Where(book => book.DealNo == 1 && book.Strategy == "Condor")
.SelectMany(book => book.GreeksLL.Where(greek => greek.BookOrComp == "Book" && greek.GreekType == "Delta")
,(book, greek) => new { Greek = greek, Book = book })
.Sum(greekAndBook => greekAndBook.Book.BookPosn * greekAndBook.Book.Fxrate * greekAndBook.Greek.Data[3]);