This is a migrated thread and some comments may be shown as answers.

LINQ Problem with multiple criteria

3 Answers 90 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Santhosh
Top achievements
Rank 1
Santhosh asked on 25 Dec 2009, 07:16 AM
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

3 Answers, 1 is accepted

Sort by
0
Zoran
Telerik team
answered on 30 Dec 2009, 10:02 AM
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.
0
Santhosh
Top achievements
Rank 1
answered on 30 Dec 2009, 10:27 AM
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.

0
Zoran
Telerik team
answered on 04 Jan 2010, 08:55 AM
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.
Tags
LINQ (LINQ specific questions)
Asked by
Santhosh
Top achievements
Rank 1
Answers by
Zoran
Telerik team
Santhosh
Top achievements
Rank 1
Share this question
or