1

I am a beginner with Entity Framework. I am building a WPF app. Following is my model class:

[Table("Vehicle")]
    public class Vehicle
    {
        [Key]
        public int VehicleId { get; set; }
        [Column("LicencePlateNumber", TypeName = "ntext")]
        [MaxLength(100)]
        public string LicencePlateNumber { get; set; }
        [Column("LicencePlateState", TypeName = "ntext")]
        [MaxLength(100)]
        public string LicencePlateState { get; set; }
        [Column("Make", TypeName = "ntext")]
        [MaxLength(100)]
        public string Make { get; set; }
        [Column("Other", TypeName = "ntext")]
        [MaxLength(100)]
        public string Other { get; set; }
        [Column("Model", TypeName = "ntext")]
        [MaxLength(100)]
        public string Model { get; set; }
        [Column("Style", TypeName = "ntext")]
        [MaxLength(100)]
        public string Style { get; set; }
        [Column("Color", TypeName = "ntext")]
        [MaxLength(100)]
        public string Color { get; set; }
        [Column("PurchaseYear", TypeName = "int")]
        public int PurchaseYear { get; set; }
        [Column("InsurnaceCompanyName", TypeName = "ntext")]
        [MaxLength(100)]
        public string InsurnaceCompanyName { get; set; }
        [Column("InsurnaceCompanyNumber", TypeName = "ntext")]
        [MaxLength(100)]
        public string InsurnaceCompanyNumber { get; set; }
    }

I am trying to retrieve data from this table in datagrid.

 using (var context = new TransportContext())
 {                
       var vehicleList = from v in context.Vehicles
                         select new
                         {
                              Number = v.LicencePlateNumber,
                              Make = v.Make,
                              Model = v.Model,
                              Year = v.PurchaseYear
                         };
                    DG_Details.ItemsSource = vehicleList;
                    DG_Details.Items.Refresh();
   }

But I am getting an XAMLParseException. On the Otherhand if I try this then it works:

 using (var context = new TransportContext())
 {              
       var projectionQuery = from v in context.Vehicles
                                  select v;
            var vehicleList = projectionQuery.ToList<Vehicle>();
                    DG_Details.ItemsSource = vehicleList;
                    DG_Details.Items.Refresh();
   }

But I want to retrieve only 4 columns not all. Need Help!!

1
  • What is your exception message? I suspect this is because your vehicleList is an anonymous type. You can try creating a class with those four properties defined and create a new instance in your linq statement. Your binding will be easier as you will have a strong type. Commented Oct 20, 2014 at 7:49

1 Answer 1

4

Try this :

var vehicleList = (from v in context.Vehicles
                         select new
                         {
                              Number = v.LicencePlateNumber,
                              Make = v.Make,
                              Model = v.Model,
                              Year = v.PurchaseYear
                         }).ToList();
                    DG_Details.ItemsSource = vehicleList;
                    DG_Details.Items.Refresh();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.