質問

I am using following linq statement in Web Api using entity framework. Everything is working fine except in my linq statement I dont know how to pass the ICollection

public class projOverview
{

    public int proj_id { get; set; }
    public string name { get; set; }
    public virtual ICollection<p_type> p_type { get; set; }   
}

And the linq statement is

   var pOver= (from c in db.pr_d
                               select new pOver
                               {
                                   name=c.proj.name,
                                   proj_id=c.proj.p_id,
                                   p_type= ICollection<c.proj.pro_type>   
                               }).Take(7);
                return pOver;

I get intellisense error on " p_type= ICollection" saying it is a type but used like a variable. Please let me know how to fix it. Thank

役に立ちましたか?

解決

The error message should be helpful, you are setting p_type to a type (ICollection<T>) instead of a variable, you need to change to this:

   var pOver = (from c in db.pr_d
                         select new ProjOverview
                           {
                               name=c.proj.name,
                               proj_id=c.proj.p_id,
                               p_type= c.p_type_variable   
                           }).Take(7);
   return pOver;

I've also noticed that you used pOver as a class name, maybe you meant to use ProjOverview?

他のヒント

when specifying a type, use the typeof operator

var pOver= (from c in db.pr_d
            select new pOver
            {
                name=c.proj.name,
                proj_id=c.proj.p_id,
                p_type= typeof(ICollection<c.proj.pro_type>)
            }).Take(7);
return pOver;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top