Sort out the code for querying by using LINQ (1)

Source: Internet
Author: User

1. Multi-condition combination query using the three-object Operator

Code

 Var emailListInfos = from e in emailCtx
Where (strEmailSubject = "select ")? True: (e. subject = strEmailSubject) & (e. senddate = null )? False: (DateTime. Compare (dateConditionL, Convert. ToDateTime (e. senddate) <= 0) & (e. senddate = null )? False: (DateTime. Compare (Convert. ToDateTime (e. senddate), dateConditionR. AddDays (1) <0 ))
Select e;

The above code queries emails by "Email Subject" and "sending time period. Because e. senddate is of the null type, a type conversion is also performed with the verification of whether it is null. Previously, dateConditionL and dateConditionR were processed as time types. The subject of the email is selected in the drop-down box. Therefore, you can determine whether the subject is the initial "Please select.

For a combined condition query like this, when there are many conditions, if you use if... else... the generated branches are very complicated. Even if you finish writing them in one breath, the amount of code will be very large. Therefore, it is much easier to replace if... else... branch with the Three-object operation.

One of the solutions found on the Internet is to introduce an extension class and directly use the and or append condition to implement dynamic queries. Here is a link: http://www.cnblogs.com/killuakun/archive/2008/08/03/1259389.html

 

2. Remove rows with duplicate data for a field in the query result.

Code

   class MyComparer : IEqualityComparer<User>         
 {
    public bool Equals(User x, User y)
    {
      if (x == null && y == null)
      {
        return false;
}
      else
      {
        return x.username == y.username;
}
    } 
    public int GetHashCode(User obj)
    {
      return obj.username.GetHashCode();
    }
 }

You can append this line of code after the query statement. asenumerable <user> (). distinct (New mycomparer (); when multiple data entries with the same name exist in the result set, only the first one appears. User is the data set to be queried.

This article is first published on my 51cto blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.