anonymous-types

LINQ GroupBy Anonymous Type

走远了吗. 提交于 2020-01-13 11:43:10
问题 I am wondering why GroupBy works with anonymous types. List<string> values = new List<string>(); values.GroupBy(s => new { Length = s.Length, Value = s }) Anonymous types do not implement any interfaces, so I am confused how this is working. I assume that the algorithm is working by creating an instance of the anonymous type for each item in the source and using hashing to group the items together. However, no IEqualityComparer is provided to define how to generate a hash or whether two

RuntimeBinderException when accessing dynamic anonymous type in view

孤人 提交于 2020-01-13 10:16:11
问题 I've encountered a strange anomaly while learning/tinkering with asp.net. I'm trying to show a partial view like this: @Html.Partial("_PartialView", new { Action = "Foo" }) When I'm trying to access Action with // Throws Microsoft.Csharp.RuntimeBinder.RuntimeBinderException string throwsException = Model.Action; a RuntimeBinderExceptionis with the message 'object' does not contain a definition for 'Action' is thrown. The strange thing is that this line works fine: // This line works fine

Are C# anonymous types redundant in C# 7

a 夏天 提交于 2020-01-12 13:59:27
问题 Since C# 7 introduces value tuples, is there a meaningful scenario where they are better suited than tuples? For example, the following line collection.Select((x, i) => (x, i)).Where(y => arr[y.i].f(y.x)).ToArray(); makes the following line collection.Select((x, i) => new {x, i}).Where(y => arr[y.i].f(y.x)).ToArray(); redundant. What would be the use case where one is better used over the other (for either performance reasons or optimization)? Obviously, if there is a need for more than six

Dynamic view of anonymous type missing member issue - MVC3

谁说胖子不能爱 提交于 2020-01-12 04:04:53
问题 I have an MVC3 site that I've setup for testing another site - most of it has been quick and dirty, and so I've not gone to town creating model and view model types for all the views - only where input is requried from the user. Okay so I have a controller method that projects a Linq sequence and sets it into ViewBag . ViewBag.SomeData = Enumerable.Range(1,10).Select(i=> new { Value = i }); In my view (Razor C#) I then want to read this - quite simple: @foreach(dynamic item in ViewBag

Why a Microsoft.CSharp.RuntimeBinder.RuntimeBinderException if the invoked method is there?

烈酒焚心 提交于 2020-01-11 04:23:04
问题 I have the following code which creates a dynamic object that is assigned to the smtpClient variable. public class TranferManager { public void Tranfer(Account from, Account to, Money amount) { // Perform the required actions var smtpClient = New.SmtpClient(); smtpClient.Send("info@bank.com", "from.Email", "Tranfer", "?"); // In the previous line I get a Microsoft.CSharp.RuntimeBinder.RuntimeBinderException // with the description = "'object' does not contain a definition for 'Send'" } }

Anonymous Types in C#

折月煮酒 提交于 2020-01-11 00:42:08
问题 // x is compiled as an int var x = 10; // y is compiled as a string var y = "Hello"; // z is compiled as int[] var z = new[] { 0, 1, 2 }; but // ano is compiled as an anonymous type var ano = new { x1 = 10, y1 = "Hello" }; ano object's properties created are read-only . I want to figure it out why those properties are read only. suggestions are appreciated ? EDIT: var ano1 = new { x1 = 10, y1 = "Hello" }; var ano2 = new { x1 = 10, y1 = "Hello" }; Is that if the new anonymous type has the same

razor view with anonymous type model class. It is possible?

半城伤御伤魂 提交于 2020-01-09 04:59:04
问题 I want to create a view using razor template, but I do not want to write a class for model, because in many views i will have many queries which will be returning diferent models. For example I have a linq query: from p in db.Articles.Where(p => p.user_id == 2) select new { p.article_id, p.title, p.date, p.category, /* Additional parameters which arent in Article model */ }; I need to write a View for this query. This query returns a Articles. Now I dont know how should looks like a model

Why can't I set properties or use attributes in C# anonymous types?

懵懂的女人 提交于 2020-01-06 19:45:10
问题 C# anonymous types seem really useful, but I've pretty immediately hit upon an application where I would like to set the property of an anonymous type, and I'd also like to be able to use attributes. My application is a general purpose stored procedure and SQL executor, which can automatically map C# properties to SQL input and output paramaters with appropriate SqlDbType (e.g. string maps to NVARCHAR(MAX) , etc.). e.g. int PersonID = 1234; var output = new { GivenName = (string)null,

Get cell values from a WPF datagrid that has been populated with anonymous types

£可爱£侵袭症+ 提交于 2020-01-06 12:36:33
问题 I have a datagrid that I populated with 2 arrays of strings by following the process as outlined in this question here Now when Im trying to get selected cell values im struggling with pulling the cell values back out. So far I have: IList rows = dgMain.SelectedItems; Which returns a list of the selected items, where the first item contains an object with the username and password. My question is how do I access the username and password? I guess I need to cast the object in the list to

Pass anonymous type as model in MVC 3 Partial View

耗尽温柔 提交于 2020-01-06 02:53:09
问题 I am refactoring an MVC 3 application, and moved a set of similar items into a partial view so I can keep that template DRY. Since the pieces don't all have the exact same properties, I am creating anonymous types like this: var model1 = new { Description = "description 1", Message = "message 1" } and passing them to the partial view like so: @Html.Partial("_Partial", model1) The partial view is then attempting to render certain blocks based on existence of a specific property, i.e. @if