P
Paolo
I've run into a problem trying to create a named class with a grouped
LINQ query and intermediate aggregations.
I created a class thus:
public class GroupedItem
{
string spacer = "\t"
public string CategoryDesc { get; set; }
public string SubCatDesc { get; set; }
public decimal Amount { get; set; }
public GroupedItem(string categoryDesc, string subCategoryDesc,
decimal amount)
{
CategoryDesc = c.C_Description,
SubCateDesc = s.S_Description,
Amount = trans.T_Amount
}
public string ToString(string decimalDisplayFormat)
{
return Category + spacer + SubCategory + spacer +
Amount.ToString(decimalDisplayFormat);
}
}
to use in the following LINQ query:
private void btnGroupedCategories_Click(object sender, EventArgs e)
{
var groupQuery =
from trans in dataSet.Transaction
from c in dataSet.Category where trans.T_Category == c.C_Id
from s in dataSet.SubCategory where trans.T_SubCategory ==
s.S_Id
select new GroupedItem
(
c.C_Description,
s.S_Description,
trans.T_Amount
)
into startGroup
group startGroup by new
{
startGroup.CategoryDesc,
startGroup.SubCatDesc
}
into secondGroup
select new
{
Category = secondGroup.Key.CategoryDesc,
SubCategory = secondGroup.Key.SubCatDesc,
Count = secondGroup.Count(),
Sum = secondGroup.Sum(s => s.Amount),
Mean = ( secondGroup.Sum(s =>
s.Amount))/(secondGroup.Count())
}
into thirdGroup
orderby thirdGroup.Category,
thirdGroup.SubCategory
select thirdGroup;
string headerText = "Column headers go here";
setRTBHeadings(headerText);
foreach (GroupedItem item in groupQuery)
{
richtxbxAnalysis.AppendText(item.ToString(decimalDisplayFormat) +
Environment.NewLine);
}
}
I am receiving an error regarding unable to convert anonymous type to
Grouped Item.
I'm assuming this is something to do with the
select new
{
Category = secondGroup.Key.CategoryDesc,
SubCategory = secondGroup.Key.SubCatDesc,
Count = secondGroup.Count(), <<<<<<<< problem??
Sum = secondGroup.Sum(s => s.Amount), <<<<<<<<
Mean = ( secondGroup.Sum(s =>
s.Amount))/(secondGroup.Count()) <<<<<<<<
}
I've got Category, SubCategory and Amount defined in my GroupedItem class
but I don't know how to deal with Count, Sum and Mean - I can't pass them as
parameters in the constructor since they don't exist at instantiation. I've
also not been able to include these in my ToString() method for the same
reason.
How do I solve this problem?
LINQ query and intermediate aggregations.
I created a class thus:
public class GroupedItem
{
string spacer = "\t"
public string CategoryDesc { get; set; }
public string SubCatDesc { get; set; }
public decimal Amount { get; set; }
public GroupedItem(string categoryDesc, string subCategoryDesc,
decimal amount)
{
CategoryDesc = c.C_Description,
SubCateDesc = s.S_Description,
Amount = trans.T_Amount
}
public string ToString(string decimalDisplayFormat)
{
return Category + spacer + SubCategory + spacer +
Amount.ToString(decimalDisplayFormat);
}
}
to use in the following LINQ query:
private void btnGroupedCategories_Click(object sender, EventArgs e)
{
var groupQuery =
from trans in dataSet.Transaction
from c in dataSet.Category where trans.T_Category == c.C_Id
from s in dataSet.SubCategory where trans.T_SubCategory ==
s.S_Id
select new GroupedItem
(
c.C_Description,
s.S_Description,
trans.T_Amount
)
into startGroup
group startGroup by new
{
startGroup.CategoryDesc,
startGroup.SubCatDesc
}
into secondGroup
select new
{
Category = secondGroup.Key.CategoryDesc,
SubCategory = secondGroup.Key.SubCatDesc,
Count = secondGroup.Count(),
Sum = secondGroup.Sum(s => s.Amount),
Mean = ( secondGroup.Sum(s =>
s.Amount))/(secondGroup.Count())
}
into thirdGroup
orderby thirdGroup.Category,
thirdGroup.SubCategory
select thirdGroup;
string headerText = "Column headers go here";
setRTBHeadings(headerText);
foreach (GroupedItem item in groupQuery)
{
richtxbxAnalysis.AppendText(item.ToString(decimalDisplayFormat) +
Environment.NewLine);
}
}
I am receiving an error regarding unable to convert anonymous type to
Grouped Item.
I'm assuming this is something to do with the
select new
{
Category = secondGroup.Key.CategoryDesc,
SubCategory = secondGroup.Key.SubCatDesc,
Count = secondGroup.Count(), <<<<<<<< problem??
Sum = secondGroup.Sum(s => s.Amount), <<<<<<<<
Mean = ( secondGroup.Sum(s =>
s.Amount))/(secondGroup.Count()) <<<<<<<<
}
I've got Category, SubCategory and Amount defined in my GroupedItem class
but I don't know how to deal with Count, Sum and Mean - I can't pass them as
parameters in the constructor since they don't exist at instantiation. I've
also not been able to include these in my ToString() method for the same
reason.
How do I solve this problem?