LINQ Problem with multiple criteria

4 posts, 0 answers
  1. santhosh
    santhosh avatar
    28 posts
    Member since:
    Mar 2009

    Posted 25 Dec 2009 Link to this post

    Dear All,

    I am using the following LINQ for retrieving the data with the specified criteria

    IList<Feature> features = (from c in scope.Extent<Project>().ToList()
                                            where c.id.ToString() == Projectid
                                               from d in c.features 
                                               where d.id.ToString() == FeatureId
                                               select d).ToList();


    Here I need to filter with ProjectID and FeatureID, this query successfully runs at the first time of my application starts,

    In the second time, this query doesnt producing the values. 

    Can anybody guide me how to fix the problem
  2. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 30 Dec 2009 Link to this post

    Hi santhosh,

    By calling ToList() in te first line of your query, OpenAccess fetches all the product objects from the database into a List. The remaining part of the query is executed by Linq To Objects(which should always return correct results) and not the OpenAccess Linq implementation. I can not tell you for sure why your query does return results only on the first run, but I could offer you a way to re-write the query so it will be executed on the database server and probably give you the desired results. I guess your Feature objects have a reference to a Product object(as the inverse collection is there). In that case you could consider the following query:
    IList<Feature> features = (from d in scope.Extent<Feature>()
                               where d.id.ToString() == FeatureId
                               && d.Product.id == ProductId
                               select d;


    Kind regards,
    Zoran
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. santhosh
    santhosh avatar
    28 posts
    Member since:
    Mar 2009

    Posted 30 Dec 2009 Link to this post

    hi Admin,

    thanks for your quick response.

    Actually, I have declared the objects like this

    Project class:

     

     [Persistent]  
       public class Project  
        {  
            public Project()  
            {  
                Features = new List<Feature>();  
            }  
            
            public int id;  
            public int ID  
            {  
                get { return id; }  
                set { id = value; }  
            }
            [Depend]  
            public IList<Feature> features;  
            public IList<Feature> Features  
            {  
                get { return features; }  
                set { features = value; }  
            }  
        }  
     

    and Feature class like this:

    [Persistent]  
        public  class Feature  
        {  
            public string stories = string.Empty;  
            public string Stories  
            {  
                get { return stories; }  
                set { stories = value;}  
            }  
            public int id;  
            public int ID  
            {  
                get { return id; }  
                set { id = value; }  
            }  
     
        } 


    In this case, i need a LINQ expression where i need to filter the results using both the Id values.

  5. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 04 Jan 2010 Link to this post

    Hello santhosh,

    In this case, the query as you had originally written it should work properly. There is only one thing left that you should change in your persistent classes. I see your fields are marked as public as well as your properties. You should mark the fields as private and leave only the properties public. Having the fields marked as public results in problems with the enhancer which could be the possible reason for the false results you are getting.

    Greetings,
    Zoran
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top