0
我有一個LINQ-功能,partialy獲取不同的方法使用,所以我認爲這將有可能以不同的部分,我會打電話,當我需要他們分裂:如何拆分Linq函數?
public static void SetTab(int tab)
{
var collection = from product in Entity.Collection.collection
let p = product as Entity.Base.Product
from version in p.version
let v = version as Entity.Base.Version
from customer in v.customerCollection
let c = customer as Entity.Base.Customer
from error in v.errorCollection
let e = error as Entity.Base.Error
select new { p, v, c, e };
Entity.Selection.products = (List<Entity.Base.Product>)collection.Select(x => x.p).Distinct().ToList();
Entity.Selection.versions = (List<Entity.Base.Version>)collection.Select(x => x.v).Distinct().ToList();
Entity.Selection.customers = (List<Entity.Base.Customer>)collection.Select(x => x.c).Distinct().ToList();
Entity.Selection.errors = (List<Entity.Base.Error>)collection.Select(x => x.e).Distinct().ToList();
Entity.Selection.categories = (List<Entity.Base.Category>)collection.Select(x => x.e.category).Distinct().ToList();
switch (tab)
{
case 0:
Entity.DataSource.MainTab.lbProduct = Converter.ListConverter.ToBindingList(Entity.Selection.products);
Entity.DataSource.MainTab.lbVersion = Converter.ListConverter.ToBindingList(Entity.Selection.versions);
Entity.DataSource.MainTab.lbCustomer = Converter.ListConverter.ToBindingList(Entity.Selection.customers);
Entity.DataSource.MainTab.lbError = Converter.ListConverter.ToBindingList(Entity.Selection.errors);
Entity.DataSource.MainTab.lbCategory = Converter.ListConverter.CategoryToBindingList(Entity.Selection.categories);
break;
}
}
這應該是這個樣子:
private static object GetFrom()
{
var collection = from product in Entity.Collection.collection
let p = product as Entity.Base.Product
from version in p.version
let v = version as Entity.Base.Version
from customer in v.customerCollection
let c = customer as Entity.Base.Customer
from error in v.errorCollection
let e = error as Entity.Base.Error
select new { p, v, c, e };
}
public static void SetTab(int tab)
{
Reduce(GetFrom());
SelectThis(0);
}
private static void Reduce(object collection)
{
Entity.Selection.products = (List<Entity.Base.Product>)collection.Select(x => x.p).Distinct().ToList();
Entity.Selection.versions = (List<Entity.Base.Version>)collection.Select(x => x.v).Distinct().ToList();
Entity.Selection.customers = (List<Entity.Base.Customer>)collection.Select(x => x.c).Distinct().ToList();
Entity.Selection.errors = (List<Entity.Base.Error>)collection.Select(x => x.e).Distinct().ToList();
Entity.Selection.categories = (List<Entity.Base.Category>)collection.Select(x => x.e.category).Distinct().ToList();
}
private static void SelectThis(int i)
{
switch (tab)
{
case 0:
Entity.DataSource.MainTab.lbProduct = Converter.ListConverter.ToBindingList(Entity.Selection.products);
Entity.DataSource.MainTab.lbVersion = Converter.ListConverter.ToBindingList(Entity.Selection.versions);
Entity.DataSource.MainTab.lbCustomer = Converter.ListConverter.ToBindingList(Entity.Selection.customers);
Entity.DataSource.MainTab.lbError = Converter.ListConverter.ToBindingList(Entity.Selection.errors);
Entity.DataSource.MainTab.lbCategory = Converter.ListConverter.CategoryToBindingList(Entity.Selection.categories);
break;
}
}
我的問題是:我需要什麼來使用,而不是「對象」從一個方法傳遞「變種集合」到另一個?
你爲什麼要執行DB查詢5不同的時間只是爲了讓每一個給定對象的5個屬性的列表?您只需執行一次查詢,然後將所有數據全部取下即可。最重要的是,您正在5個不同的表之間進行交叉連接,以便您可以提取所有單獨的不同值。這是*超級*浪費數據庫資源。爲什麼不把每個列表拉下來,而不是將每個列表與其他4個列表結合起來,而忽略所有其他值? – Servy