anonymous-types

Is there any difference between `new object()` and `new {}` in c#?

僤鯓⒐⒋嵵緔 提交于 2020-01-03 08:54:13
问题 in c#, var x = new {}; declares an anonymous type with no properties. Is this any different from var x = new object(); ? 回答1: Yes, the types used are different. You can tell this at compile-time: var x = new {}; // Won't compile - no implicit conversion from object to the anonymous type x = new object(); If you're asking whether new{} is ever useful - well, that's a different matter... I can't immediately think of any sensible uses for it. 回答2: Well, for starters, object is an actual, non

Is there any difference between `new object()` and `new {}` in c#?

ⅰ亾dé卋堺 提交于 2020-01-03 08:53:34
问题 in c#, var x = new {}; declares an anonymous type with no properties. Is this any different from var x = new object(); ? 回答1: Yes, the types used are different. You can tell this at compile-time: var x = new {}; // Won't compile - no implicit conversion from object to the anonymous type x = new object(); If you're asking whether new{} is ever useful - well, that's a different matter... I can't immediately think of any sensible uses for it. 回答2: Well, for starters, object is an actual, non

Linq to SQL: DISTINCT with Anonymous Types

非 Y 不嫁゛ 提交于 2020-01-01 07:28:51
问题 Given this code: dgIPs.DataSource = from act in Master.dc.Activities where act.Session.UID == Master.u.ID select new { Address = act.Session.IP.Address, Domain = act.Session.IP.Domain, FirstAccess = act.Session.IP.FirstAccess, LastAccess = act.Session.IP.LastAccess, IsSpider = act.Session.IP.isSpider, NumberProblems = act.Session.IP.NumProblems, NumberSessions = act.Session.IP.Sessions.Count() }; How do I pull the Distinct() based on distinct Address only? That is, if I simply add Distinct(),

Other than for LINQ queries, how do you use anonymous types in C#?

徘徊边缘 提交于 2020-01-01 05:29:29
问题 I've been trying to get up to speed on some of the newer features in C# and one of them that I haven't had occasion to use is anonymous types. I understand the usage as it pertains to LINQ queries and I looked at this SO post which asked a similar question. Most of the examples I've seen on the net are related to LINQ queries, which is cool. I saw some somewhat contrived examples too but not really anything where I saw a lot of value. Do you have a novel use for anonymous types where you

Is there a trick in creating a generic list of anonymous type?

拥有回忆 提交于 2019-12-31 08:05:36
问题 Sometimes i need to use a Tuple, for example i have list of tanks and their target tanks (they chase after them or something like that ) : List<Tuple<Tank,Tank>> mylist = new List<Tuple<Tank,Tank>>(); and then when i iterate over the list i access them by mylist[i].item1 ... mylist[i].item2 ... It's very confusing and i always forget what is item1 and what is item2, if i could access them by : mylist[i].AttackingTank... mylist[i].TargetTank... It would be much clearer, is there a way to do it

Emitting an HTML string from anonymous type property in a repeated, dynamically-typed Partial View

拜拜、爱过 提交于 2019-12-31 03:45:17
问题 I am passing an anonymous type into a dynamic partial view as part of the @model , and one of the properties is a string that contains some HTML. When I use the HtmlHelper methods to render the property, the Razor engine is encoding the string, resulting in literal text on the page - <i>text</i> in this case, instead of the desired text . Since it is a dynamically typed View, I cannot call the property directly. Specifically, if I try to bind to @Model.MyField , I get a

Entity Framework object limitations in aggregate LINQ query

人盡茶涼 提交于 2019-12-31 03:32:12
问题 I have a rather complicated query that I'd like to have return some particular types. Mostly concerning date / time calculations or string values, the Entity Framework seems to be spitting the dummy when I try anything further than a System.DateTime type: The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. The query in question is: // Here comes the aggregate query. object summary = from te

How to return a generic list collection in C#?

橙三吉。 提交于 2019-12-30 09:44:11
问题 I have some linq to sql method and when it does the query it returns some anonymous type. I want to return that anonymous type back to my service layer to do some logic and stuff on it. I don't know how to return it though. I thought I could do this public List<T> thisIsAtest() { return query; } but I get this error Error 1 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?) So not sure what assembly I am missing or if that is even

IQueryable for Anonymous Types

那年仲夏 提交于 2019-12-30 08:15:33
问题 I use EntityFramework, I'm querying and returning partial data using Anonymous Types. Currently I'm using IQueryable<dynamic> , it works, but I would like to know if this is the proper way to do it or if there is some other returning datatype that I'm not aware of. public IQueryable<dynamic> FindUpcomingEventsCustom(int daysFuture) { DateTime dateTimeNow = DateTime.UtcNow; DateTime dateTimeFuture = dateTimeNow.AddDays(daysFuture); return db.EventCustoms.Where(x => x.DataTimeStart >

Can you Instantiate an Object Instance from JSON in .NET?

爷,独闯天下 提交于 2019-12-30 04:31:11
问题 Since Object Initializers are very similar to JSON, and now there are Anonymous Types in .NET. It would be cool to be able to take a string, such as JSON, and create an Anonymous Object that represents the JSON string. Use Object Initializers to create an Anonymous Type: var person = new { FirstName = "Chris", LastName = "Johnson" }; It would be awesome if you could pass in a string representation of the Object Initializer code (preferably something like JSON) to create an instance of an