Optimize code in LINQ

Posted by Santosh4u under LINQ on 4/8/2016 | Points: 10 | Views : 1957 | Status : [Member] | Replies : 5
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
Output:
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

my code for above output.
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)
var FinalCompanyInvoice = CompanyInvoiceList.Union(CompanyEmployInvoice).ToList();
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 i




Responses

Posted by: Rajnilari2015 on: 4/10/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
please format your code properly for readability purpose.

--
Thanks & Regards,
RNA Team

Santosh4u, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santosh4u on: 4/10/2016 [Member] Bronze | Points: 25

Up
0
Down
Hi i have created a list<T> with some sample data . refer the code as attached document.just copy the code to see the output.

Thanks
 Download source file

Santosh4u, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 4/10/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Put a check and then perform the operation

Hope that helps

--
Thanks & Regards,
RNA Team

Santosh4u, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santosh4u on: 4/13/2016 [Member] Bronze | Points: 25

Up
0
Down
Hi Rajnilari,
i have attached again.please try .

Thanks
Santosh
 Download source file

Santosh4u, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santosh4u on: 4/18/2016 [Member] Bronze | Points: 25

Up
0
Down
Hi Rajnilari,
hope you managed to download the attachment and refer the code. let me know if you have any idea in this.

Thanks

Santosh4u, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response