c# - Processing 2 lists -



c# - Processing 2 lists -

i have xml file read , process data. have list of details record. requirement remove records same number , refno have positive , negative values.

scenario 1 : should homecoming 1 record

<detail> <number>1</number> <amount>20.0</amount> <refno>1</refno> </detail>

scenario 2 : should homecoming 0 record because amount positive , negative

<detail> <number>1</number> <amount>20.0</amount> <refno>1</refno> </detail> <detail> <number>1</number> <amount>-20.0</amount> <refno>1</refno> </detail>

i have achieved using 2 list positive , negative values. solved above scenarios using next code.

var actualrecords = (from detaillist in positivedetails !negativedetails.any(x => x.number == detaillist.policyno && x.refno == detaillist. refno) select detaillist).tolist();

but above code homecoming 0 records in next scenario. positive > negative > positive. should homecoming 1 record cancelling 1 positive , 1 negative record same number , refno.

i want solution next scenarios same number , ref no.

• positive > negative > positive – 1 record • positive > negative > positive > negative – 0 record • positive > negative > positive > negative > positive – 1 record • positive > negative > positive > negative > positive > negative – 0 record • positive > negative > positive > negative > positive > n

assumption negative appear after positive values. can loops. i’m searching improve solution. appreciate if can help me.

are simply aggregating details or trying match details cancel each other out exactly? former easier write.

e.g., document looks this

<root> <details> <detail> <number>1</number> <amount>40.0</amount> <refno>1</refno> </detail> <detail> <number>1</number> <amount>-20.0</amount> <refno>1</refno> </detail> <detail> <number>1</number> <amount>20.0</amount> <refno>1</refno> </detail> <detail> <number>1</number> <amount>-30.0</amount> <refno>2</refno> </detail> <detail> <number>1</number> <amount>20.0</amount> <refno>1</refno> </detail> </details> </root> xdocument doc = ...; var details = doc.xpathselectelement("/root/details"); var newdetails = detail in details.elements("detail") allow amount = (decimal)detail.element("amount") allow number = (int)detail.element("number") allow refno = (int)detail.element("refno") // grouping amounts number , refno grouping amount new { number, refno } g allow amount = g.sum() // filter out canceled out groups amount != 0m select new xelement("detail", new xelement("number", g.key.number), new xelement("amount", amount.tostring("n1")), new xelement("refno", g.key.refno) ); details.replaceall(newdetails);

yields:

<root> <details> <detail> <number>1</number> <amount>60.0</amount> <refno>1</refno> </detail> <detail> <number>1</number> <amount>-30.0</amount> <refno>2</refno> </detail> </details> </root>

otherwise, need match elements cancel each other out. can still done in single query, though more complicated one.

xdocument doc = ...; var details = doc.xpathselectelement("/root/details"); var newdetails = detail in details.elements("detail") allow amount = (decimal)detail.element("amount") allow number = (int)detail.element("number") allow refno = (int)detail.element("refno") allow key = math.abs(amount) // cancellable amounts // grouping amounts key, number , refno grouping amount new { key, number, refno } g allow amount = g.sum() // filter out canceled out groups amount != 0m allow count = (int)math.abs(amount / g.key.key) // how many recreate allow sign = math.sign(amount) // recreate uncancelled values in enumerable.repeat(sign * g.key.key, count) select new xelement("detail", new xelement("number", g.key.number), new xelement("amount", a.tostring("n1")), new xelement("refno", g.key.refno) ); details.replaceall(newdetails);

yields:

<root> <details> <detail> <number>1</number> <amount>40.0</amount> <refno>1</refno> </detail> <detail> <number>1</number> <amount>20.0</amount> <refno>1</refno> </detail> <detail> <number>1</number> <amount>-30.0</amount> <refno>2</refno> </detail> </details> </root>

c# linq

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -