What wrong I am doing in IEnumerable(C#3.0)
If I write
var v = (from r in stock.ReplacementLog
select new
开发者_StackOverflow {
AssetId = stock.AssetId,
Date = stock.ReferDate,
FactType = r.Key,
Value = r.Value
});
It is working fine...
But if I do
IEnumerable<StockAsset> v = (from r in stock.ReplacementLog
select new {
AssetId = stock.AssetId,
Date = stock.ReferDate,
FactType = r.Key,
Value = r.Value });
I am getting error:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)
Then I did
IEnumerable<StockAsset> v =
(
from r in stock.ReplacementLog
select new
{
AssetId = stock.AssetId,
Date = stock.ReferDate,
FactType = r.Key,
Value = r.Value
}).ToList<StockAsset>();
With the following bunch of errors:
Error 1 Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable'
Error 2 'System.Collections.Generic.IEnumerable' does not contain a definition for 'ToList' and the best extension method overload 'System.Linq.Enumerable.ToList(System.Collections.Generic.IEnumerable)' has some invalid arguments
Then I tried with
IEnumerable<StockAsset> v1 =
(from r in stock.ReplacementLog
select new StockAsset
{
AssetId = stock.AssetId,
ReferDate= stock.ReferDate,
FactType = r.Key,
Value = r.Value
});
with the errors: Error 1 'StockAsset' does not contain a definition for 'FactType'
**Error 2
'StockAsset' does not contain a definition for Value'**
The StockAsset Class is as under
public class StockAsset
{
public int AssetId { get; set; }
public DateTime ReferDate {get;set;}
public Dictionary<EnumFactorType, double> ReplacementLog { get; set; }
}
Need help.
Using C#3.0
Thanks
When you write
select new {
AssetId = stock.AssetId,
Date = stock.ReferDate,
FactType = r.Key,
Value = r.Value }
You actually generate an anonymous type. You can't cast this anonymous type to a declared type.
If you want to create an object of the class you should do
select new StockAsset
{
AssetId = ..., // Maybe stock.AssetId
ReferDate = ..., // Maybe stock.ReferDate
ReplacementLog = ... // Maybe new Dictionary<string, short> { {r.Key, r.Value} };
}
When you write
select new { property = value }
you're creating an instance of a new anonymous type, whereas it looks like you want to actually be creating StockAsset
s, so you want
select new StockAsset { property = value }
My best guess is that you're trying to create a new StockAsset for each entry in another StockAsset's ReplacementLog. This new StockAsset is going to have a single entry in its ReplacementLog. Is this correct? If so, you could create a new constructor on StockAsset:
public StockAsset(int assetId, DateTime referDate,
EnumFactorType factorType, double value)
{
AssetId = assetId;
ReferDate = referDate;
ReplacementLog = new Dictionary<EnumFactorType, double>();
ReplacementLog[factorType] = value;
}
And then call that constructor inside of your LINQ.
IEnumerable<StockAsset> v = (from r in stock.ReplacementLog
select new StockAsset(stock.AssetId, stock.ReferDate, r.Key, r.Value));
精彩评论