使用C#在本地保存查询结果 [英] Saving query results locally in C#

查看:106
本文介绍了使用C#在本地保存查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在我的Web应用程序中,我希望赋予用户向下钻取信息的能力.首先,用户可以通过单击汽车制造商来查看汽车制造商和型号,然后可以查看有关汽车的更多信息,例如年份,颜色等.然后,通过单击汽车年份,用户可以看到更多信息.如里程,电子测试等.

我的问题是我该如何提前提取有关汽车的所有信息并将其保存在本地对象中,这样,通过单击链接(向下钻取),我不必向数据库提交新查询即可提取数据.相关信息.我只想能够查询本地对象而不是数据库.

任何帮助将不胜感激!

谢谢,

Jerry

Hello all,

In my web application, I want to give the user the ability to drill down the information. At first the user sees a car make and model, by clicking on the car make, the user can then see more information about the car such as year, color, etc. Then by clicking on the car year, the user can see more information like milage, e-test, and etc.

My question is how can I pull all the information about the car in advance and save it in a local object so that by clicking on the links (drilling down) I don''t have to submit a new query to the database to pull the relevant information. I just want to be able to query the local object instead of the database.

Any help would be appreciated!

Thanks,

Jerry

推荐答案

使用SessionState 是解决方案.

让我们假设您有一个Car 对象,该对象具有一些基本属性以及CarDetail object属性,如下所示:

Using SessionState is the solution.

Let us assume that you have a Car object which some basic properties, along with a CarDetail object property, something like as follows:

public class Car
{
  public string Make {get;set;}
  public string Model {get;set;}
  //Other basic properties
  public CarDetail {get;set;}
}



CarDetail 类可能具有一些详细信息属性,如下所示:



The CarDetail class may have some detail properties as follows:

public class Car
{
  public int Year {get;set;}
  public Coler coler {get;set;}
  //Other detail properties...
}



而且,CarDetail 可能还有另一个对象.

我们将此称为对象图,其中Car 对象包含其他对象(CarDetail),该对象可能包含其他对象作为属性.

因此,您要做的是,首先加载页面时,您将从数据库中加载请求的Car 的所有基本和详细信息,并在Car 对象图中填充数据.然后,将Car 对象存储在Session中.对于以后的页面请求,可以从Session提供Car 对象数据.

这是执行此操作的示例方法:



And, the CarDetail may have another objects as it''s property.

Let''s call this an object graph, where the Car object contains other object (CarDetail) which may contain other object(s) as property.

So, what you have to do is, when your page is loaded first, you load all basic and detailed information for the requested Car from the database and populate the data in the Car object graph. Then, you store the Car object in the Session. For later page request, you serve the Car object data from the Session.

Here is a sample method that does this:

protected Car GetCar(int carId)
{
    Car car = null;
    if(Session["Car"] == null)
    {
        car = GetCar(carId); //Populate the Car object graph from database
        Session["Car"] =  car; //Store in the session
    }
    else
    {
        car = Session["Car"] as Car; //Get the Car object from Session
    }
    return car;
}



希望对您有帮助



Hope, this helps


好吧,一旦您检索到数据,它就位于DataTable中,对吗?因此,只需使用LINQ来查询DataTable对象.
Well, once you retrieve the data, it resides in a DataTable, correct? So just use LINQ to query the DataTable object.


这篇关于使用C#在本地保存查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆