views:

154

answers:

3

If I write

var v = (from r in stock.ReplacementLog
                                             select new 
                                             {
                                                 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

A: 

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 StockAssets, so you want

select new StockAsset { property = value }
AakashM
I am getting error:Error 1 'StockAsset' does not contain a definition for 'Date'Error 2'StockAsset' does not contain a definition for 'FactType'Error 3'StockAsset' does not contain a definition for Value'when I am doingIEnumerable<StockAsset> v1 = (from r in stock.ReplacementLogselect new StockAsset {AssetId = stock.AssetId,Date = stock.ReferDate,FactType = r.Key,Value = r.Value});
Newbie
StockAsset doesn't have a property named Value. Are you really trying to create a StockAsset object, and if so, what is the Value supposed to be?
Adam Ruth
+2  A: 

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} };
 }
brickner
Unfortunately the StockAsset class doesn't have Date, FactType or Value properties, so that would still give an error.
Adam Ruth
Actually yes... So how to overcome that
Newbie
@Adam Ruth, thanks. I've fixed the answer.
brickner
Error 1 'StockAsset' does not contain a definition for 'Date' Error 2'StockAsset' does not contain a definition for 'FactType'Error 3'StockAsset' does not contain a definition for Value'
Newbie
@Newbie, you should decide how do you want to create the values for the properties of StockAsset.
brickner
Sir, The output I have generated using the VAR(as described in the question)
Newbie
If I understood, you meanIEnumerable<StockAsset> v1 = (from r in stock.ReplacementLogselect new StockAsset {AssetId = stock.AssetId,Date = stock.ReferDate,ReplacementLog = r}); and the error:Error 1 Cannot implicitly convert type 'System.Collections.Generic.KeyValuePair< EnumFactorType,double>' to 'System.Collections.Generic.Dictionary< EnumFactorType,double>
Newbie
@Newbie, look at the comments I've added.
brickner
ReplacementLog = new Dictionary<EnumFactorType,double>(new[]{r})errorArgument '1': cannot convert from 'System.Collections.Generic.KeyValuePair< EnumFactorType,double>[]' to 'int'
Newbie
@Newbie, I've fixed the comment.
brickner
A: 

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));
Adam Ruth