Hello Guys, I hope you are all doing fine. I have a Linq to SQL problem.
My entity class is as follows;

using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace SilverlightGrid.Web.TableMappings

    [Table(Name = "Travel_Meal")]
    public class Travel_MealDetails
        [Column(IsPrimaryKey = true)]
        public Guid MealID { get; set; }

        [Column(Name = "Meal_Desc")]
        public String MealType { get; set; }


It works fine but I get an error when I query data like so:

public List<Travel_MealDetails> getMeals()
            Settings db3 = new Settings();
           // DataClasses1DataContext db = new DataClasses1DataContext("NucleusConnectionString");
            DataContext db2 = new DataContext(db3.DBConnect);
            Table<Travel_MealDetails> meals = db2.GetTable<Travel_MealDetails>();

            var persons = (from c in meals
                           select new Travel_MealDetails
                               MealType = c.MealType,
                               MealID = c.MealID

            return persons.ToList();

It throws an error saying:
Explicit construction of entity type 'SilverlightGrid.Web.TableMappings.Travel_MealDetails' in query is not allowed.
I hope someone can help me TIA.

Can you convert the Table<Travel_MealDetails> to a List<Travel_MealDetails> before running the Linq?

Also (after that), your linq can just return a List<Travel_MealDetails>

return (from c in lst_meals
   select new ...

...you won't need to call .ToList() twice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.