0

I need to enhance a function of C# code. I am new to C# but I have strong java background.

My job is to write a new query, like JDBC, to get data from database.

In the method below, I didn't see any SQL query.

what does this line mean ? is this similar to Hibernate hql ?

from p in Session.Query<MyObject>() select p

Thanks

code:

 public IPagingList<MyObject> ReadMyObjectItems(int start, int limit, IList<Filter> filters)
        {

            var criteria = Session.CreateCriteria<MyObject>();

            if (limit != -1)
            {
                criteria.SetMaxResults(limit);
                criteria.SetFirstResult(start);
            }

            if (filters != null)
            {
                foreach (var filter in filters)
                {
                    criteria.Add(Restrictions.InsensitiveLike(filter.Field, "%" + filter.Value + "%"));
                }
            }

            IList<MyObject> report = criteria.List<MyObject>();

            int total = (from p in Session.Query<MyObject>() select p).Count();

            var pagedResults = new PagingList<MyObject> { List = report, Total = total };

            return pagedResults;

        }
1
  • It's similar in concept to hql, to find more information on this, you'll want to look up linq to nhibernate or nhibernate linq provider. Commented Apr 30, 2012 at 19:45

3 Answers 3

1

(from p in Session.Query<MyObject>() select p).Count();

We are simply getting a count of all the objects.

Refactor this to

Session.Query<MyObject>().Count(). It's easier to read and in this case LINQ is unnecessary.

Sign up to request clarification or add additional context in comments.

Comments

1

It's Linq syntax. A powerful form of querying incorporated into .Net and different .Net frameworks.
In your case it seems you are using NHibernate and that line is using NHibernate Linq. (But the surrounding code is using Criteria.)

For composing queries in NHibernate you have the options of using

  • Linq
  • HQL
  • Criteria

which all have different pros and cons.
Either will be translated into SQL by NHibernate. The actual result will be fetched when you try to access it such as for instance reading an item in the result or converting the result to a List etc.

Comments

1

It seems that project is using an ORM like NHibernate. You can get more details here : http://nhibernate.info

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.