首页 > 解决方案 > 如何在类中缓存查询结果,然后根据 C# 方法中传递的参数重用它进行搜索?

问题描述

我正在尝试在 ASP.NET MVC Web 应用程序中实现搜索属性功能,其中页面具有体验列表,并且每个体验都有分配给它的唯一 ID 并具有属性列表。当用户单击一个体验时,它将显示与该体验相关的属性。现在,当用户单击另一个体验时,它将同时获取 id 并返回公共属性。我有一个缓存数据库结果并存储它的类。

public class GetExperienceResponse
{
    public string Experienceid { get; set; }
    public List<string> Properties { get; set; }
   //Example: Expereienceid="E1", Properties="P1,P2,P3" 
              Experienceid="E2",  Properties="P3,P4,P5"
}


public static List<GetExperienceAmenityResponse> GetExperience()
{
//returns experience and related properties here and cached it 
}

我根据点击的经验用于搜索属性的方法是:

public ActionResult ExperienceSearch(string id)
    {
        List<GetExperienceResponse> Experiences  = new List<GetExperienceResponse>();
        Experiences = GetExperience(); //Populate the Experience result list
       //logic for searching. When Click on Experience1, it will pass id "E1" and should display Properties "P1", "P2" and "P3". When Click on both Experience it should display common property which is "P3" here (Intersection of Experience 1 and  Experience 2).
        return View();
    }

实现此搜索的最佳方法是什么?我可以对返回的结果使用 LINQ 查询吗?我将不胜感激。非常感谢您!

标签: c#asp.netasp.net-mvc

解决方案


个人体验可以有唯一的 Cachekey,公共属性可以有共同的缓存键(Experienceid1 &Experienceid2 的hashkey)。


推荐阅读