有没有办法指定匿名空枚举类型?

发布于 2024-09-12 11:28:26 字数 600 浏览 12 评论 0原文

我返回一个 Json 化的匿名类型:

IList<MyClass> listOfStuff = GetListOfStuff();

return Json(
   new {
       stuff = listOfStuff
   }
);

在某些情况下,我知道 listOfStuff 将为空。所以我不想要调用 GetListOfStuff() (这会进行数据库调用)的开销。

所以在这种情况下我写的是:

return Json(
   new {
       stuff = new List<ListOfStuff>()
   }
);

这似乎有点不必要的冗长。我不在乎它是什么类型的 List ,因为它无论如何都是空的。

是否有可用于表示空可枚举/列表/数组的简写?比如:

return Json(
   new {
       stuff = new []
   }
);

或者我编写 JavaScript 的时间太长了? :)

I'm returning a Json'ed annonymous type:

IList<MyClass> listOfStuff = GetListOfStuff();

return Json(
   new {
       stuff = listOfStuff
   }
);

In certain cases, I know that listOfStuff will be empty. So I don't want the overhead of calling GetListOfStuff() (which makes a database call).

So in this case I'm writing:

return Json(
   new {
       stuff = new List<ListOfStuff>()
   }
);

which seems a bit unnecessarily verbose. I don't care what type of List it is, because it's empty anyway.

Is there a shorthand that can be used to signify empty enumerable/list/array? Something like:

return Json(
   new {
       stuff = new []
   }
);

Or have I been programming JavaScript too long? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

清风挽心 2024-09-19 11:28:26

本质上你想发出一个空数组。 C# 可以从参数推断数组类型,但对于空数组,您仍然需要指定类型。我想你原来的做法已经足够好了。或者你可以这样做:

return Json(
   new {
       stuff = new ListOfStuff[]{}
   }
);

数组的类型并不重要,因为任何空的枚举都会转换为 JSON 中的 [] 。我想,为了可读性,请指定空数组的类型。这样,当其他人阅读您的代码时,该成员应该是什么就更明显了。

Essentially you want to emit an empty array. C# can infer the array type from the arguments, but for empty arrays, you still have to specify type. I guess your original way of doing it is good enough. Or you could do this:

return Json(
   new {
       stuff = new ListOfStuff[]{}
   }
);

The type of the array does not really matter as any empty enumerable will translate into [] in JSON. I guess, for readability sake, do specify the type of the empty array. This way when others read your code, it's more obvious what that member is supposed to be.

夏了南城 2024-09-19 11:28:26

在处理匿名类型时,dynamic也是一个更好的选择,

Enumerable.Empty<dynamic>()

这对我来说效果很好

dynamic is also a better option when dealing with an anonymous type

Enumerable.Empty<dynamic>()

this worked well for me

蓝戈者 2024-09-19 11:28:26

您可以使用 Enumerable.Empty 更明确一点:

return Json(
    new {
        stuff = Enumerable.Empty<ListOfStuff>()
    }
);

尽管它并不短并且没有摆脱类型参数。

You could use Enumerable.Empty to be a little more explicit:

return Json(
    new {
        stuff = Enumerable.Empty<ListOfStuff>()
    }
);

Although it isn't shorter and doesn't get rid of the type argument.

横笛休吹塞上声 2024-09-19 11:28:26

您可能不关心它是什么类型的列表,但这对调用者来说很重要。 C# 通常不会尝试根据存储变量来推断类型(就像无法在返回类型上创建方法重载一样),因此有必要指定类型。也就是说,如果您想返回空数组,可以使用 new ListOfStuff[0] 。这对于调用者来说(长度)是不可变的(如果他们尝试调用长度可变的 IList 方法,他们会得到一个异常。)

You might not care what type of list it is, but it matters to the caller. C# does not generally try to infer types based on the variable to which it is being stored (just as you can't create overloads of methods on return type), so it's necessary to specify the type. That said, you can use new ListOfStuff[0] if you want an empty array returned. This has the effect of being immutable (in length) to the caller (they'll get an exception if they try to call the length-mutating IList<T> methods.)

猛虎独行 2024-09-19 11:28:26

是的,有。您必须定义一个至少包含一个元素的数组,并使用 linq 过滤该数组,不留下任何元素。例子:

var foo = new
{
    Code = 1,
    Name = "Bar",
    Value = (float?)5.0
};

//use an empty object (or any object) to define the type of the array
var emptyArrayOfFooType = new[] {
    new
    {
        Code = (int)0,
        Name = (string)null,
        Value = (float?)null
    }
}.Where(e => false).ToArray(); //use linq to filter the array leaving no elements

//you can use an existing anonymous type variable too
var anotherEmptyArray = new[] { foo }.Where(e => false).ToArray();

//this array with one element has the same type of the previous two arrays
var fooArray = new[] { foo };

//all arrays have the same type, so you can combine them
var combinedArrays = emptyArrayOfFooType.Concat(anotherEmptyArray).Union(fooArray);

Yes, there is. You have to define an array with as least one element, and use linq to filter the array leaving no elements. Example:

var foo = new
{
    Code = 1,
    Name = "Bar",
    Value = (float?)5.0
};

//use an empty object (or any object) to define the type of the array
var emptyArrayOfFooType = new[] {
    new
    {
        Code = (int)0,
        Name = (string)null,
        Value = (float?)null
    }
}.Where(e => false).ToArray(); //use linq to filter the array leaving no elements

//you can use an existing anonymous type variable too
var anotherEmptyArray = new[] { foo }.Where(e => false).ToArray();

//this array with one element has the same type of the previous two arrays
var fooArray = new[] { foo };

//all arrays have the same type, so you can combine them
var combinedArrays = emptyArrayOfFooType.Concat(anotherEmptyArray).Union(fooArray);
一指流沙 2024-09-19 11:28:26

发送通用类型?:

List<Object>()

或发送空对象数组:new Object[0]

send a generic type?:

List<Object>()

or send an empty object array: new Object[0]

北城孤痞 2024-09-19 11:28:26

我猜你在这里谈论的是 C#。我的 C# 知识有限,但我不认为你可以创建一个没有类型的新对象。为什么不能返回通用的 new List[] ? (这里可能混合了 Java 泛型,我不确定是否可以在 C# 中返回泛型类型列表)。

I guess you are talking about C# here. My knowledge is limited in C# but I don't think you can create a new object with no type. Why can't you return a generic new List[] ? (might be mixing Java generics here, am not sure is one can return a generic type list in C#).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文