Converting an object T into a List<T> in C#
I have a stupid yet braincracker issue about casting types. As you can see from the code I have a variable lprod_monthylReport that depending on the ytm value can be a List<Monthly_Report>
or simply a Monthly_Report
. I need the variable to have the same name in both cases.
var lprod_monthlyReport = new List<Monthly_Report>;
if (ytm == true)
{
lprod_monthlyReport = _ProductRep.GetSpecificArchiveReport(prod.Prod_ID, lmonth, lyear, item.item_ID);
}
else
lprod_monthlyReport = 开发者_运维技巧_ProductRep.GetSpecificYTMReport(prod.Prod_ID, item.item_ID);
The problem is that if I declare the variable inside each if (or else) section the compilers gives error because it says that the variable is already declared in this context.
I already tried casting
lprod_monthlyReport = (Monthly_Report) _ProductRep.GetSpecificArchiveReport(prod.Prod_ID, lmonth, lyear, item.Item_ID);
But it does not work. I also tried the as keyword without success.
May you please help me with this one? Thanks
Francesco
This should work:
in case you've got a Monthly_Report
just add it to the list...
List<Monthly_Report> lprod_monthlyReport;
if (ytm == true)
{
lprod_monthlyReport = new List<Monthly_Report>();
lprod_monthlyReport.add(_ProductRep.GetSpecificArchiveReport(prod.Prod_ID, lmonth, lyear, item.item_ID));
}
else{
lprod_monthlyReport = _ProductRep.GetSpecificYTMReport(prod.Prod_ID, item.item_ID));
}
The first line of code
var lprod_monthlyReport = new List<Monthly_Report>;
means that the variable is of type List<Monthly_Report>
so it cannot contain a single Monthly_Report
object.
This code should be refactored to work differently or have that one method return a List<Monthly_Report>
with only one Monthly_Report
in it.
The important thing about the var
keyword is that it doesn't mean "variant". var
is used for type inference by the compiler, which means where you declare:
var something = new List<Something>();
...you are declaring something to be of type List<Something>
. This means you can't then do:
something = new Something();
... as you've already declared it as something else. var
from a usability perspective is syntactic sugar. The compiler (and the Visual Studio intellisense engine) are using type inference to know that something
is actually List<Something>
.
This is not going to work, alas.
But you could as alternative wrap the single instance into a list if you wanted. Then you only have to make your code deal with a List<Monthly_Report>
of either one or more Monthly_Report
items.
declare lprod_monthlyReport
as an 'object'. There is no is
relationship between Monthly_Report
and List<Monthly_Report>
.
You will have issues when you come to use the 'lprod_monthlyReport' because you won't know what type it is, other than object, the 'is' keyword could help you then.
Consider that and think about restructuring your code.
Try this
List<Monthly_Report> lprod_monthlyReport;
if (ytm == true)
{
lprod_monthlyReport = _ProductRep.GetSpecificArchiveReport(prod.Prod_ID, lmonth, lyear, item.item_ID);
}
else
{
lprod_monthlyReport = new List<Monthly_Report>()
{
_ProductRep.GetSpecificYTMReport(prod.Prod_ID, item.item_ID)
};
}
精彩评论