none
unable to create a constant value of type RRS feed

  • Question

  • In the first, my project is OK.

    A few days ago, I add some entity to database (use Entity Framework 6.0, Code First), everystep is OK.

    And then, it occurred this problem.

    The Example Code is:

    class A
    {
        public int Id{get;set;}
    }
    
    class B
    {
        public int Id{get;set;}
        public int Aid{get;set;}
        public virtual A A{get;set;}
    }
    
    //下面的Linq就报告错误:
    
    Bs.Where(x=>x.A==a).FirstOrDefault();

    Thanks!

    • Moved by CoolDadTx Monday, September 29, 2014 4:35 PM EF related
    Friday, September 26, 2014 8:34 AM

Answers

  • Is that what you wanted ?

     A a = new A();
                B[] Bs = new B[2];
                a.Id = 5;
    
                try { 
                Bs[0] = new B();
                Bs[0].A = new A();
                Bs[0].A.Id=8;
                Bs[0].Id = 32;
    
                Bs[1] = new B();
                Bs[1].A = new A();
                Bs[1].A.Id = 5;
                Bs[1].Id = 45;
    
                B b =  Bs.Where(x=>x.A.Id==a.Id).FirstOrDefault();
                    
                }
                catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
    


    "If there's nothing wrong with me, maybe there's something wrong with the universe!"

    • Marked as answer by 王当文 Sunday, March 15, 2015 2:39 AM
    Friday, September 26, 2014 9:49 AM
  • My bet is on the x.A == a code.  Use the ID instead because if you're using EF then it has to translate down to a SQL call and object references aren't going to work at the SQL level.  If that doesn't resolve your problem then please post the line where the exception or compiler error is occurring.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by 王当文 Sunday, March 15, 2015 2:38 AM
    Monday, September 29, 2014 4:35 PM

All replies

  • What seems to be the problem ?

                A a = new A();
                B[] Bs = new B[5];
                
                B b =  Bs.Where(x=>x.A==a).FirstOrDefault();


    "If there's nothing wrong with me, maybe there's something wrong with the universe!"

    Friday, September 26, 2014 9:41 AM
  • Is that what you wanted ?

     A a = new A();
                B[] Bs = new B[2];
                a.Id = 5;
    
                try { 
                Bs[0] = new B();
                Bs[0].A = new A();
                Bs[0].A.Id=8;
                Bs[0].Id = 32;
    
                Bs[1] = new B();
                Bs[1].A = new A();
                Bs[1].A.Id = 5;
                Bs[1].Id = 45;
    
                B b =  Bs.Where(x=>x.A.Id==a.Id).FirstOrDefault();
                    
                }
                catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
    


    "If there's nothing wrong with me, maybe there's something wrong with the universe!"

    • Marked as answer by 王当文 Sunday, March 15, 2015 2:39 AM
    Friday, September 26, 2014 9:49 AM
  • Thanks!

    In the first, the project is OK.

    The Query is EF Lambda.

    Sunday, September 28, 2014 3:27 AM
  • Thanks!

    In the first, the project is OK.

    The Query is EF Lambda.

    Sunday, September 28, 2014 3:27 AM
  • My bet is on the x.A == a code.  Use the ID instead because if you're using EF then it has to translate down to a SQL call and object references aren't going to work at the SQL level.  If that doesn't resolve your problem then please post the line where the exception or compiler error is occurring.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by 王当文 Sunday, March 15, 2015 2:38 AM
    Monday, September 29, 2014 4:35 PM