Best way to optimize code in LINQ
Hi,i have very simple requirement , i have already written the code and it's working properly, since i am very new to LINQ, i feel that the way i have written the code may be correct way, please have a look on below and do provide me the optimize code.
my list as below
SourceType SourceField DestinationType DestinationField
// below two items direct relation with invoice
Company Company1 Invoice Invoice1
Company Company2 Invoice Invoice2
// below item INdirect relation with invoice
Company Company2 Employee Employee1
Employee Employee1 Invoice Invoice3
// below item INdirect relation with invoice
Company Company2 Employee Employee2
Employee Employee2 Invoice Invoice4
// below item INdirect relation with invoice
Company Company1 Employee Employee2
Employee Employee2 Invoice Invoice1
// below item no relation with invoice so ignore
Company Company4 Employee Employee10
Employee Employee15 Invoice Invoice1
i am getting above record in a list called CompanyList, i want the below output from list
[B]Output:[/B]
Company1 Invoice1
Company2 Invoice2
Company2 Invoice3
Company2 Invoice4
Company2 Invoice4
Company2 Invoice1
Company1 Invoice1
Basically want get all the company list which having direct/indirect relation with invoice
i'e if company matches with invoice then it will return // (Item1 and 2) company have direct relation with invoice
if company matches with employee and same employee matches with invoice also will return // company indirect relation with invoice
[B]my code for above output.[/B]
var CompanyInvoiceRelation = CompanyList.Where(r => r.SourceType == "Company" && r.DestinationType.ToString() == "Invoice").ToList();
var CompanyEmployeeRelation = CompanyList.Where(r => r.SourceType.ToString() == "Company" && r.DestinationType.ToString() == "Employee").ToList();
var EmployeeInvoiceRelation = CompanyList.Where(r => r.SourceType.ToString() == "Employee" && r.DestinationType.ToString() == "Invoice").ToList();
List<CompanyInvoice> NodeConnectionDestinationCustomersIds = new List<CompanyInvoice>();
var CompanyInvoiceList = CompanyInvoiceRelation.Select(p => new CompanyInvoice(){Company = p.SourceField,Invoice = p.DestinationField}).ToList();
List<CompanyInvoice> CompanyEmployInvoice= new List<CompanyInvoice>();
foreach (var customer in CompanyEmployeeRelation)
{
if ((from c in EmployeeInvoiceRelation where c.SourceField == customer.DestinationField select c).Any() == false)
continue;
var EmployeeInvoiceList = (from c in EmployeeInvoiceRelation where c.SourceField == customer.DestinationField select c);
foreach (var item in EmployeeInvoiceList)
{ var Item = new CompanyInvoice()
{
Company = customer.SourceField,Invoice = item.DestinationField};
CompanyEmployInvoice.Add(Item); } }
//Merging direct relation list with indirect relation list (using union)
[B] var FinalCompanyInvoice = CompanyInvoiceList.Union(CompanyEmployInvoice).ToList();[/B]
FinalCompanyInvoice.Select(r => new { Company = r.Company, Invoice = r.Invoice}).Distinct().ToList();
2. now i want to check each record of above list with one table which contains columns(Company,invoice,Amount) if record found then it will get the amount from table and update to amount field of list if not matching then amount will be 1.
My Try :
i am getting all record from table into a list(DBList) then looping through FinalCompanyInvoice list if found any item(DBList) based on company and invoice then updating list amount filed with DBList.amount or updating as 1
i hope this is clear, let me know if not very clear about the requirement then i will post you a sample application.
Thanks