I have some duplicate code using LINQ & XML. I'm sure there is a way to refactor it, but I'm not sure how to go about doing it. Would someone help, please?
var fileInfo = dataXL.Worksheet("data")
.Where(t => t["F2"].Equals(company))
.Select(t => new
{
Price = t["F5"],
EPS10Q1 = t["F12"], EPS10Q2 = t["F13"], EPS10Q3 = t["F14"], EPS10Q4 = t["F15"],
EPS11Q1 = t["F19"], EPS11Q2 = t["F20"], EPS11Q3 = t["F21"], EPS11Q4 = t["F22"],
EPS12Q1 = t["F26"], EPS12Q2 = t["F27"], EPS12Q3 = t["F28"], EPS12Q4 = t["F29"],
EPS13Q1 = t["F33"], EPS13Q2 = t["F34"], EPS13Q3 = t["F35"], EPS13Q4 = t["F36"],
EPS14Q1 = t["F40"], EPS14Q2 = t["F41"], EPS14Q3 = t["F42"], EPS14Q4 = t["F43"]
});
xdoc.Element("company")
.Add(new XElement("eps",
new XElement("year2010",
new XElement("EPS1", resAnGreyInfo.Select(x => x.EPS10Q1)),
new XElement("EPS2", resAnGreyInfo.Select(x => x.EPS10Q2)),
new XElement("EPS3", resAnGreyInfo.Select(x => x.EPS10Q3)),
new XElement("EPS4", resAnGreyInfo.Select(x => x.EPS10Q4))),
new XElement("year2011",
new XElement("EPS1", resAnGreyInfo.Select(x => x.EPS11Q1)),
new XElement("EPS2", resAnGreyInfo.Select(x => x.EPS11Q2)),
new XElement("EPS3", resAnGreyInfo.Select(x => x.EPS11Q3)),
new XElement("EPS4", resAnGreyInfo.Select(x => x.EPS11Q4))),
new XElement("year2012",
new XElement("EPS1", resAnGreyInfo.Select(x => x.EPS12Q1)),
new XElement("EPS2", resAnGreyInfo.Select(x => x.EPS12Q2)),
new XElement("EPS3", resAnGreyInfo.Select(x => x.EPS12Q3)),
new XElement("EPS4", resAnGreyInfo.Select(x => x.EPS12Q4))));
t["F2"] == company
? If yes, you're going to get weird results. If not,fileInfo
should be a single object, not a collection. – svick Jun 24 at 18:40.Single()
after yourSelect()
. – svick Jun 24 at 19:16