user5710892
user5710892

Reputation:

Unable to convert AnonymousType#1 ti IEnumerable

I have declared a model in my .cs page

public class InstrumentDetails
{
    public long PullNo { get; set; }
    public string Description { get; set; }
    public int New { get; set; }
    public int LN { get; set; }
    public int PR { get; set; }
    public int Any { get; set; }
}

After that i am retrieving data like this way :

IEnumerable<InstrumentDetails> instrumentdetails = (from p in NemcDb.tblPulls
    join
    pi in NemcDb.tblPullInstruments
    on
    p.PullId equals pi.PullId
    join
    i in NemcDb.tblInstruments
    on
    pi.InstrumentCode equals i.InstrumentCode
    select new
    {
        PullNo = p.PullNo.Value,
        InstrumentType = i.Description,
        New = pi.NewQuantity,
        LN = pi.LNQuantity,
        PR = pi.UsedQuantity,
        Any = pi.AnyQuantity
    }).Where(i => i.PullNo.ToString() == item);

But it gives me the error

Cannot convert 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<>'. An explicit conversion exists (are you missing a cast?)

Where am I wrong?...I couldn't get it through....Please help.

Upvotes: 0

Views: 438

Answers (2)

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31143

You don't tell that you want an object of type InstrumentDetails, you create an anonymous type. Change select new { ... into select new InstrumentDetails { ... and then also call ToList(), ToEnumerable() or ToArray() in the end so the query is actually run.

Upvotes: 2

user5710892
user5710892

Reputation:

The answer would be :

            IEnumerable<InstrumentDetails> instrumentdetails = (from p in NemcDb.tblPulls
                                                            join
                                                            pi in NemcDb.tblPullInstruments
                                                            on
                                                            p.PullId equals pi.PullId
                                                            join
                                                            i in NemcDb.tblInstruments
                                                            on
                                                            pi.InstrumentCode equals i.InstrumentCode
                                                            select new InstrumentDetails
                                                            {
                                                                PullNo = p.PullNo.Value,
                                                                InstrumentType = i.Description,
                                                                New = pi.NewQuantity,
                                                                LN = pi.LNQuantity,
                                                                PR = pi.UsedQuantity,
                                                                Any = pi.AnyQuantity

                                                            }).Where(i => i.PullNo.ToString() == item).ToList();

    }

How Silly of me............

Upvotes: 0

Related Questions