LINQ - new select error - Asked By shekhar kumar on 07-Jun-11 02:26 AM

I have two classes as follows
 public class Make
    {
      public int MakeId { get; set; }
      public string MakeName { get; set; }
      public Model Model = new Model();
      public Make()
      {
      }
      public Make(int makeID, string makeName, int modelID, string modelName)
      {
        MakeId = makeID;
        MakeName = makeName;
        Model.ModelId = modelID;
        Model.ModelName = modelName;
      }
    }
    public class Model
    {
      public int ModelId { get; set; }
      public string ModelName { get; set; }
    }

Now i want to write a select statement as follows

 List<Make> makes = new List<Make>();
        makes.Add(new Make(1, "Nokia", 1, "N 74"));
        makes.Add(new Make(1, "Nokia", 2, "N 75"));

        var collection1=from m in makes select new(m.MakeId,m.MakeName,m.Model.ModelId, m.Model.ModelId);

It is giving me an error "TYPE EXPECTED". Where I am doing wrong.

Thanks for the help.
Shekhar
Riley K replied to shekhar kumar on 07-Jun-11 02:44 AM
Replace your stmnt with my below given stmnt

  var collection1 = from m in makes select new { m.MakeId, m.MakeName, m.Model.ModelId };

in your stmnt you have declare m.Nodel.ModelId twice and also use curly braces

Thanks
shekhar kumar replied to Riley K on 07-Jun-11 03:10 AM
Thanks for the help :).
It worked for me.

Jitendra Faye replied to shekhar kumar on 15-Jun-11 11:25 PM
You are getting error because of this line-

var collection1=from m in makes select new(m.MakeId,m.MakeName,m.Model.ModelId, m.Model.ModelId);

In this lile you are selecting more that one fiels using select statement,
so when you select more that one field that you have to use {} with select, but you have used ().

Change you rcode like this -


var collection1=from m in makes select new{m.MakeId,m.MakeName,m.Model.ModelId, m.Model.ModelId};

Try and let me know.