Vi. Querying dataIn C # Redis Combat (V) describes how to delete data in Redis, this article will continue to describe the query in Redis.
1. Using LINQ to match keyword queries
[CSharp]View Plain copy
- using (var redisclient = redismanager.getclient ())
- {
- var user = redisclient.gettypedclient<user> ();
- var userlist = user. GetAll (). Where (x = X.job.position.contains (Txtscreenposition.text)). ToList ();
- if (Userlist.count > 0)
- {
- var htmlstr = string. Empty;
- foreach (Var u in userlist)
- {
- Htmlstr + = "<li>id=" + u.id + "Name:" + u.name + "department:" + u.job.position + "</li>";
- }
- Lblpeople.text = Htmlstr;
- }
- Lblshow.text = "Filter after total:" + userList.Count.ToString () + "People!" ";
- }
The code above implements the function of matching the department to query the data, enter. NET, and filter out one of the Java departments. The query results are as follows:
2
, through key to query data in Redis, you can query all the key,c# in the current database by entering the keys * to achieve the same effect with the following code.
[CSharp]View Plain copy
- var user = redisclient.gettypedclient<user> ();
- var userkeylist = user. Getallkeys ();
all keys are saved in the Userkeylist code .
careful readers will find that the number of keys queried here differs from the amount of data stored in the database
we explain each of the following:Seq:user: The ID self-increment sequence used to maintain the current type of User as the object's unique ID, which is why the getnextsequence () function is used to get the latest ID for the current database.
ids:user: A list of all object IDs in the same type of user, equivalent to an index that contains all the IDs of the same type user;
urn:user:1: This is the save Enter in Redis-client: Get urn:user:1 to get JSON type data.
with the above instructions, we can easily query the value by key, the code is as follows:
[CSharp]View Plain copy
- using (var redisclient = redismanager.getclient ())
- {
- var keyValue = string. Empty;
- Try
- {
- var user = redisclient.gettypedclient<user> ();
- var value = user. GetValue (Txtkey.text);
- KeyValue + = "id=" + value. Id + "Name:" + value. Name + "department:" + value. Job.position;
- }
- catch (Exception ex)
- {
- KeyValue + = ex. ToString ();
- }
- Lblpeople.text = Keyvalue.tojson ();
- Lblshow.text = string. Empty;
- }
The effect is as follows:
C # Redis Combat (vi)