我有一個 LINQ 運算式關聯兩個物件類,兩個“表”,我在其中創建了一個 Join 運算式來關聯一些物件。我的代碼是這樣的:
list = (from x in relacaoProdutosFilial
join y in _listaDeVendas on new { x.CodigoEmpresa } equals new { y.CodigoEmpresa }
where y.RelacaoProdutos.Contains(x.CodigoProduto)
select new DetalhamentoVendaRealizada
{
<code>
}
我知道WHERE運算式不正確,但推理是這樣的,我需要過濾class Y位于class X.
我可以在 LINQ 運算式中創建這個過濾器嗎?
uj5u.com熱心網友回復:
好的,所以你有一個包含RelacaoProdutosFilial(無論是什么)的表。此表中的每個 RelacaoProdutoFilial 都有屬性CodigoEmpresa(公司代碼)和CodigoProduto(產品代碼)
您還有一張桌子ListasDeVendas(銷售清單?)。ListaDeVendas此表中的每個都有屬性CodigoEmpresa和RelacaoProdutos(產品串列)
這兩個表之間似乎存在關系。唉,你忘了給我們關系,因為你也忘了告訴我們相關的領域,特別是主鍵和外鍵,我不得不假設一些事情。
在我看來,RelacaoProdutosFilial 和 ListaDeVendas 之間似乎存在一對多關系:每個RelacaoProdutoFilial都有零個或多個ListaDeVendas,每個都ListaDeVenda屬于一個 RelacaoProdutoFilial,即外鍵CodigoEmpresa所指的那個。
此外,您給了我們一些不符合您要求的代碼,并要求我們給您一些作業代碼:
我需要過濾屬于 Y 類的 RelacaoProdutos 串列中的記錄,其中包含 X 類中的 CodigoProduto 專案。
我認為,您需要所有 RelacaoProdutosFilial,每個 RelacaoProdutoFilial 及其 ListasDeVendas。您不想要這個 RelacaoProdutoFilial 的所有 ListasDeVendas,只需要那些至少有一個 RelacaoProduto 等于 RelacaoProdutoFilial 的 CodigoProduto 的 ListasDeVendas。
換句話說,假設你有 RelacaoProdutoFilial [10] 有 CodigoProduto [34] 和幾個 ListasDeVendas:
- ListaDeVendas [20],與 RelacaoProdutos {30, 34, 36, 34}
- ListaDeVendas [21],與 RelacaoProdutos {31, 34},
- ListaDeVendas [22],與 RelacaoProdutos {30, 36}
我需要過濾屬于 Y 類的 RelacaoProdutos 串列中的記錄,其中包含 X 類中的 CodigoProduto 專案。
因此,作為輸出,您需要帶有三個 ListasDeVendas 的 RelacaoProdutoFilial [10]:
- ListaDeVendas [20],與 RelacaoProdutos {34, 34}
- ListaDeVendas [21],與 RelacaoProdutos {34},
- ListaDeVendas [22],帶有 RelacaoProdutos 和空串列
天哪,花了一些時間才找到你想要的。下次您提出問題以準確定義您的要求時,請考慮一下。畢竟,你必須把它們放在某個地方,所以為什么不告訴他們呢?它可以防止我們假設錯誤的事情
var result = RelacaoProdutosFilials.GroupJoin(ListasDeVendas,
relacaoProdutoFilials => relacaoProdutoFilials.CodigoEmpresa, // take the primary key
listaDeVendas => listaDeVendas.CodigoEmpresa, // take the foreign key
// parameter resultSelector: from every relacaoProdutoFilial, and all its
// listaDeVendas make one new:
(relacaoProdutoFilial, listasDeVendasOfThisRelacaoProdutoFilial) => new
{
// select ony the relacaoProdutoFilial that you plan to use:
Id = relacaoProdutoFilial.CodigoEmpresa,
Name = relacaoProdutoFilial.Name,
ProductCode = relacaoProdutoFilial.CodigoProduto,
...
// you don't want all listaDeVendas Of This RelacaoProdutoFilial,
// only those that have at least one RelacaoProduto that equals the
// RelacaoProdutoFilial's CodigoProduto
ListasDeVendas = listasDeVendasOfThisRelacaoProdutoFilial
.Where (listaDeVendas => listaDeVendas.RelacaoProdutos
.Contains(relacaoProdutoFilial.CodigoProduto))
.Select(listaDeVendas => new
{
// Select only the properties of this ListaDevenda that you plan to use
Id = listaDeVendas.Id,
Date = listaDeVendas.Date,
// no need to Select the foreign key, you already know the value
// CodigoEmpresa = listaDeVendas.CodigoEmpresa,
// no need to select the product code, you already know the value
// ProductCode = listaDeVendas.RelacaoProdutos,
})
.ToList(),
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/344059.html
上一篇:如何獲得第二個逗號的索引?
