Computed Columns in EF using LINQ

c# entity-framework linq

Question

With the following code;

  using (var context = new FINSAT613Entities())
        {
            gridControl1.ForceInitialize();



            DateTime endtime= new DateTime(2013, 03, 29, 15, 49, 54);
            Text = "endtime:"+endtime.ToShortDateString();


            var query =
                from A in context.A
                join B in context.B on A.ID equals B.ID
                join C in context.C on A.ID2 equals C.ID2
                where A.endtime> endtime && A.Chk.StartsWith("320")
                select new
                       {
                         A.ID,B.FOO,C.BAR etc...
                       };


            BindingSource.DataSource = query;
            gridControl1.DataSource = BindingSource;
        }

How can i add computed columns to it?(multiples a.bar with b.foo for example)

Tried using partial class but no luck with it.

    public partial class A
{
    public decimal Calculated
    {
        get { return 15; }
    }
}

The exact error i get is :

{"The specified type member 'Computed' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."}

1
0
4/4/2013 8:10:23 AM

Popular Answer

You can create class, wich has got all the fields you need: id, foo, bar, etc. and add to it Calculated field, then just modify your query:

var query =
                from A in context.A
                join B in context.B on A.ID equals B.ID
                join C in context.C on A.ID2 equals C.ID2
                where A.endtime> endtime && A.Chk.StartsWith("320")
                select new YourNewClass
                {
                    Foo = A.foo,
                    Bar = B.bar,
                    ... ,
                    Calculated = A.foo * B.bar   
                };

EDITED: if you have got a lot of fields, you can use automapper

Here is an example. Say, you have got class User from your DB and you have got class UserModel, in which you added a field FullName which constist from fields Name and Surname. So you want to create an object UserModel from object User but not to copy all the fields exclicitly. That's how you can do it:

class Program
    {
        static void Main(string[] args)
        {
            User u = new User { Name = "My", Surname = "Name" };
            Mapper.CreateMap<User, UserModel>().ForMember(dest => dest.FullName, o => o.MapFrom(src => string.Format("{0} {1}", src.Name, src.Surname)));

            UserModel um = Mapper.Map<User, UserModel>(u);
            Console.WriteLine(um.FullName);
        }
    }

    public class User
    {
        public string Name { get; set; }
        public string Surname { get; set; }
    }

    public class UserModel
    {
        public string Name { get; set; }
        public string Surname { get; set; }
        public string FullName { get; set; }
    }
2
4/4/2013 9:23:42 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow