Groups > Asp .Net > Data Access and ObjectDataSource Control > Re: Linq solution




Linq solution

Linq solution
Wed, 2 Apr 2008 05:18:31 +0000
I have crated a partial class for the contextDiagram.. I want to use the methods
togheter.. Bu it doesnt work when I call them 
            DataClassesDataContext db = new DataClassesDataContext();
            var q = db.SearchCategory(category, categorytype);
            //q += db.SearchCounty(county, municipality);

Is there anything wrong with my code?? or how can i fix my problem... 

 

public partial class DataClassesDataContext
    {
        public IQueryable SearchItems(params string[] keywords)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            IQueryable query = db.Items;

            foreach (string keyword in keywords)
            {
                string temp = keyword;
                query = query.Where(p => p.Description.Contains(temp));
            }
            return query;
        }

        public IQueryable SearchCategory(int categoryID, int subCategoryID)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            IQueryable query = db.Items;

            if(subCategoryID > 0)
            {
                query = query.Where(p => p.CategoryTypeId == subCategoryID);
            }
            else if (categoryID > 10)
            {
                query = query.Where(p => p.SubCategory_Type.SubCategoryId ==
categoryID);
            }
            else if(categoryID > 0 && categoryID < 10)
            {
                query = query.Where(p =>
p.SubCategory_Type.SubCategory.CategoryId == categoryID);
            }
            
            return query;
        }

        public IQueryable SearchCounty(int countyID, int municipalityID)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            IQueryable query = db.Items;

            if (municipalityID > 0)
            {
                query = query.Where(p => p.Member.MunicipalityId ==
municipalityID);
            }
            else if (countyID > 0)
            {
                query = query.Where(p => p.Member.Municipality.CountyId ==
countyID);
            }

            return query;
        }
    }  





 

            DataClassesDataContext db = new DataClassesDataContext();
            var q = db.SearchCategory(categorytype, categorytype);
            //q += db.SearchCounty(county, municipality);

            var query = from items in q
                        orderby items.DateTimeStamp descending
                        select new
                        {
                            ItemId = items.ItemId,
                            DateTimeStamp = items.DateTimeStamp,
                            Headline = items.Headline,
                            Price = items.Price,
                            Municipality =
items.Member.Municipality.Municipality1,
                            CategoryType = items.SubCategory_Type.Type
                        };
Post Reply
Re: Linq solution
Thu, 3 Apr 2008 04:09:26 +0000
I need a bit expertise help... 

I have 4 dropdownlists and a searchtextbox. If none of the controls are filled
then it gets all the data from databases.. and let us say if I have choosen a
category then it should get the correct category.. and so on

And I have created a class as you can see above, to check different statmensts.
How can I get use of this.. I want to check the methods id the user has filles
searchword or category.. if he has then The methods return the query as above..
but I cant use them togheter.. 

FOr example.. string query = "Select * from products"

                       query += "And category = " +
categorydropdownlist;

                      guery += "And Contains " + searchword

and then execute the query

this is a short example. What i mean is that I want to extend my query with the
return I get from the methods.
Post Reply
Re: Linq solution
Thu, 3 Apr 2008 08:01:58 +0000
Hi Nezdet,

For your requirements, I think List<ofType> will help you resovle the
question. 

Since you're using entity type, you can return the result as List<Item>
instead of IQueryable in every Search method. In this case, you can get the
result as type of List<Item>. Then you can combine the List<Item>
via AddRange method. It's easy to achieve that. Take a try and if there's any
question, please feel free to let me know. 

 

Thanks.
Post Reply
Re: Linq solution
Thu, 3 Apr 2008 19:36:25 +0000
Thanks for your answer


I don't get it how List and  addrange will give me correct answer.. cause it
doesnt work for me.. can you show me a example how I can use List?


Why Can't I just extend querys by adding new where statments...


And one other thing I want to crete a method-Fultextemetothod that
ExecutesQuery() do you have any soultions how to create a fulltext??
Post Reply
Re: Linq solution
Fri, 4 Apr 2008 06:22:29 +0000
Hi Nezdet,

Since you want to store the queries and use it later, you can use Session to
store the query and filter it later. Please look at the following snippet:

// Filter the query by ID and then store the result into SessionTestDataContext
ctx = newTestDataContext();

var query = ctx.Users.Where(p=>p.accountID != 2 && p.accountID!=3);

Session["query"] = query.ToList<User>();

 

// Retrieve the result from Session and filter it by Name, then store the query
into Session again for later use.List<User> users =
(List<User>)Session["query"];

var query = users.Where(p => p.userName.Trim() == "Jiang");

Session["query"] = query.ToList<User>();

 

Hope this helps you!
Post Reply
<< Previous 1 2 Next >>
( Page 1 of 2 )
about | contact