Vi. query data in the 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 Plaincopy
- 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
, using key to query dataIn 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 Plaincopy
- 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, used as the object's unique ID, that is, using the getnextsequence () function to get the latest ID of 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, and the ability to implement getall<user> () is easily implemented because such a grouping information is maintained.
Urn:user:1: This is the key that holds the user object. 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 Plaincopy
- 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:
If you want to reprint, please specify the source, this series of blog sample program
C # Redis Combat (vi)