我怎么可以定义匿名类型? [英] How could i define anonymous type?

查看:136
本文介绍了我怎么可以定义匿名类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我填充这样的(我有两个变量 jsonDataCache jsonData 但我匿名类型变量想只有一个, jsonData 为例):

  VAR jsonData =新
            {
                总=总页数,
                页=页,
                记录=总记录,

                行数=(从B中的myData
                选择新
                        {
                        //做的事情
                        })。的ToArray()
             };
返回JSON(jsonData,JsonRequestBehavior.AllowGet);
 

但是,如果我想先定义这个变量,之前,我做填充的,基本上我想要做这样的事情:

  VAR jsonData = NULL;

//检查jsonData在缓存中,如果是返回JSON(jsonData,JsonRequestBehavior.AllowGet);

jsonData =新
                {
                    总=总页数,
                    页=页,
                    记录=总记录,

                    行数=(从B中的myData
                    选择新
                            {
                            //做的事情
                            })。的ToArray()
                 };
//把jsonData缓存者皆

返回JSON(jsonData,JsonRequestBehavior.AllowGet);
 

我怎么能这样做呢?

我想这样做,因为我要为大家介绍的缓存,所以我需要定义变量第一蚂蚁比检查在缓存中,如果不是我会做以上事情的原因。 目前,我这样做有两个变量,BOT我想只使用一个。

下面是我目前做到这一点:

 公共虚拟JsonResult GetSomething(INT ID,INT型)
{
   字符串键preFIX = GetKey preFIX(ID,类型);

   VAR jsonDataCache = CacheManager.Get<对象>(键preFIX);

    如果(jsonDataCache!= NULL)
      返回JSON(jsonDataCache,JsonRequestBehavior.AllowGet);

    VAR的myData = GetFromDataase();

    VAR jsonData =新
                    {
                        总=总页数,
                        页=页,
                        记录=总记录,

                        行数=(从B中的myData
                        选择新
                                {
                                //做的事情
                                })。的ToArray()
                     };

    CacheManager.Set<对象>(键preFIX,jsonData);

    返回JSON(jsonData,JsonRequestBehavior.AllowGet);
}
 

更新: 毕竟你们的帮助,我认为它应该是类似的东西,希望这是正确的方式去:

 公共虚拟JsonResult GetSomething(INT ID,INT型)
{
   字符串键preFIX = GetKey preFIX(ID,类型);

   VAR jsonData = CacheManager.Get<对象>(键preFIX);

    如果(jsonData!= NULL)
      返回JSON(jsonData,JsonRequestBehavior.AllowGet);

    VAR的myData = GetFromDataase();

    jsonData =新
                    {
                        总=总页数,
                        页=页,
                        记录=总记录,

                        行数=(从B中的myData
                        选择新
                                {
                                //做的事情
                                })。的ToArray()
                     };

    CacheManager.Set<对象>(键preFIX,jsonData);

    返回JSON(jsonData,JsonRequestBehavior.AllowGet);
}
 

解决方案

您不能做到这一点(和初始化为null),除了一些真正的肮脏和丑陋的实例的策略与通用方法。变量匿名类型只能声明旁边初始化(除非是作为一般类型参数)。

它,然而,似乎不太可能,你在这里真正需要的。事实上,

 对象jsonData;
 

就足够了,因为你永远的使用的值。除此之外,我认为现在是时候引入一个DTO类为目的,而不是。

I have anonymous type variable which i am populating like that(i have two variables jsonDataCache and jsonData but i want to have just one, jsonData for example) :

 var jsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,

                rows = (from b in myData
                select new
                        {
                        //do things
                        }).ToArray()
             };
return Json(jsonData, JsonRequestBehavior.AllowGet);

But if i want to define this variable first, before i do populate that, basically i want to do something like that:

var jsonData = null;   

//check if jsonData in cache and if it is return Json(jsonData, JsonRequestBehavior.AllowGet);

jsonData = new
                {
                    total = totalPages,
                    page = page,
                    records = totalRecords,

                    rows = (from b in myData
                    select new
                            {
                            //do things
                            }).ToArray()
                 };
//put jsonData in cache by key

return Json(jsonData, JsonRequestBehavior.AllowGet);

How could i do that?

The reason i want to do that because i want to introduce the cache, so i need to define the variable first ant than check that in cache and if it is not i will do the thing above. Currently i did that with two variables, bot i want to use just one.

Here is how i currently done that:

public virtual JsonResult GetSomething(int id, int type)
{
   string keyPrefix = GetKeyPrefix(id, type);

   var jsonDataCache = CacheManager.Get<object>(keyPrefix);   

    if(jsonDataCache != null)
      return Json(jsonDataCache, JsonRequestBehavior.AllowGet);

    var myData = GetFromDataase();

    var jsonData = new
                    {
                        total = totalPages,
                        page = page,
                        records = totalRecords,

                        rows = (from b in myData
                        select new
                                {
                                //do things
                                }).ToArray()
                     };

    CacheManager.Set<object>(keyPrefix, jsonData);

    return Json(jsonData, JsonRequestBehavior.AllowGet);
}

UPDATE: After all your help I think it should be something like that, hopefully it is right way to go:

public virtual JsonResult GetSomething(int id, int type)
{
   string keyPrefix = GetKeyPrefix(id, type);

   var jsonData = CacheManager.Get<object>(keyPrefix);   

    if(jsonData != null)
      return Json(jsonData , JsonRequestBehavior.AllowGet);

    var myData = GetFromDataase();

    jsonData = new
                    {
                        total = totalPages,
                        page = page,
                        records = totalRecords,

                        rows = (from b in myData
                        select new
                                {
                                //do things
                                }).ToArray()
                     };

    CacheManager.Set<object>(keyPrefix, jsonData);

    return Json(jsonData, JsonRequestBehavior.AllowGet);
}               

解决方案

You can't do that (and initialize to null), except for some really nasty and ugly "by example" tactics with generic methods. Variables to anonymous types can only be declared alongside initialisation (except as a generic type parameter).

It does, however, seem unlikely that you really need it here. Indeed,

object jsonData;

would suffice, since you never use the value. Beyond that, I'd argue that it is time to introduce a DTO class for the purpose instead.

这篇关于我怎么可以定义匿名类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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