J. Davidson
J. Davidson

Reputation: 3307

Inserting IEnumerable collection in an object using Linq

Hi i have following classes

public class bizOrg
{
    public string name { get; set; }
    public int p_id { get; set; }
    public virtual product product { get; set; }
    public IEnumerable<newStructure> newStructure { get; set; }
}

And

public class newStructure
{
    public int project_id { get; set; }
    public string a_name { get; set; }
    public string a_phone { get; set; }
    public virtual product product { get; set; }
}

I have following Linq statement

 var acc = (from c in db.p_account
                            where c.p_id == id
                            select new bizOrg
                            {
                                p_id = c.product.id,
                                name = c.name,
                               newStructure=  new newStructure
                                  {    
                                    a_name = c.name,
                                    a_phone = c.phone,                                 
                                   }
                            });

On the above linq statement I get intelicense error saying

Cannot implicitly convert type newStructure to 'System.Collections.Generic.IEnumerable<newStructure>' . An explicit conversion exists

Please let me know how to correct this error. Thanks

Upvotes: 0

Views: 68

Answers (3)

ps2goat
ps2goat

Reputation: 8475

You would have to create a new IEnumerable<newStructure> in your linq statement. It expects a collection of objects, where you are only assigning a single object instead of a collection of a single object.

var acc = (from c in db.p_account
                            where c.p_id == id
                            select new bizOrg
                            {
                                p_id = c.product.id,
                                name = c.name,
                               newStructure=  new List<newStructure>{
                                    new newStructure{    
                                      a_name = c.name,
                                      a_phone = c.phone,                                 
                                     }
                                   }
                            });

Upvotes: 0

D Stanley
D Stanley

Reputation: 152521

newStructure needs to be a collection. If you just have a flat list (meaning the underlying collection will just have one item), then you can do:

 var acc = (from c in db.p_account
            where c.p_id == id
            select new bizOrg
            {
                p_id = c.product.id,
                name = c.name,
               newStructure=  new List<newStructure>
                  { new newStructure()   
                    {  
                       a_name = c.name,
                       a_phone = c.phone,                                 
                     }
                   }
            });

If there needs to be some other organization (e.g. group by name or some other property), then you'll need to provide more detail in the desired output.

Upvotes: 2

evanmcdonnal
evanmcdonnal

Reputation: 48086

This bit of code;

            select new bizOrg
                        {
                            p_id = c.product.id,
                            name = c.name,
                           newStructure=  new newStructure
                              {    
                                a_name = c.name,
                                a_phone = c.phone,                                 
                               }
                        });

Is creating a single newStructure instance and trying to assign it to a property of type IEnumerable<newStructure> instead do;

    newStructure = new { new newStructure() { a_name = c.name, a_phone = c.phone } }

which just puts your singleton into a collection, using a static initializer.

Upvotes: 0

Related Questions