Getting error while using LINQ

2 posts, 0 answers
  1. Gabriel
    Gabriel avatar
    4 posts
    Member since:
    Aug 2010

    Posted 29 Oct 2010 Link to this post

    Hi
    I have a TestClass which defines as follows:
    [Telerik.OpenAccess.Persistent(IdentityField = "_id")]
        public class TestClass
        {
            private int _id;
            private int _parentID;
            private string _nameValue;
         
            public int ID
            {
                get
                {
                    return _id;
                }
                set
                {
                    _id = value;
                }
            }
     
            public int ParentID
            {
                get
                {
                    return _parentID;
                }
                set
                {
                    _parentID = value;
                }
            }
     
            public string NameValue
            {
                get
                {
                    return _nameValue;
                }
                set
                {
                    _nameValue = value;
                }
            }
        }

    I wanted to fetch some records from database using LINQ so i used the following code:
    IObjectScope scope = ObjectScopeProvider1.ObjectScope();
     
    var result = from c in scope.Extent<TestClass>()
                 where c.NameValue.Matches("Test")
                 select c;
    objectView1.DataSource = result.ToList();

    but i get this run-time error for the last line of code:
    An exception occured during the execution of '
    Extent<WindowsFormsApplication2.TestClass>.Where(c => c.NameValue.Matches("Test"))'. See InnerException for more details.

    The strange thing is that when i omit the "where c.NameValue.Matches("Test")" line, everything works fine.

    Can anybody tell me what's the problem?
  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 01 Nov 2010 Link to this post

    Hi Gabriel,

    You should set the FieldAlias attribute for the NameValue property to be able to use this property in a query:

    [Telerik.OpenAccess.Persistent(IdentityField = "_id")]
    public class TestClass
    {
        private int _id;
        private int _parentID;
        private string _nameValue;
      
        public int ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
      
        public int ParentID
        {
            get
            {
                return _parentID;
            }
            set
            {
                _parentID = value;
            }
        }
      
        [Telerik.OpenAccess.FieldAlias("_nameValue")]
        public string NameValue
        {
            get
            {
                return _nameValue;
            }
            set
            {
                _nameValue = value;
            }
        }
    }

    Hope that helps.

    Kind regards,
    Damyan Bogoev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
Back to Top