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