anonymous-types

How to order a IEnumerable<T> of anonymous type?

See the code below, I don't know why my ordering is not working, any ideas? var orderSample = new { ProductName = "", Qty = 0, UserFullName = "" }; var ordersList = (new[] { orderSample }).ToList(); //loop thru another collection and fill ordersList by adding an order at a time ordersList.Add(new { ProductName = "Product1", Qty = 5, Us...

C# Linq Guid Anonymous Type Problem

Hello, I have a linq query which returns the ID of a question based on the questions text. This ID is then needed to be used to relate a date in a date table to that particular question. The question is already stored and the date is stored at a different time. The problem is that the query returns the questionID as an anonymous type a...

change datagrid columns order or index

OK, This is something that I cannot believe I have not been able to figure out -- please tell me I am missing someting simple... I have a datagrid, I am filling it with Linq as well as a custom class to add data to it. Afterwards, I need the data in a specific order - it seems to ignore me. How do I change a columns properties, like ...

Anonymous class initialization in VB.Net

i want to create an anonymous class in vb.net exactly like this: var data = new { total = totalPages, page = page, records = totalRecords, rows = new[]{ new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}}, new {id = 2,...

how to convert this line into vb.net

from the free dinner book for asp.net MVC [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, FormCollection formValues) { Dinner dinner = dinnerRepository.GetDinner(id); UpdateModel(dinner); dinnerRepository.Save(); return RedirectToAction("Details", new { id = dinner.DinnerID }); } how to convert this line...

Is it possible to return IEnumerable of anonymous objects from DataContext.ExecuteQuery?

I develop a reporting engine where reports are based on templates. Every template has string with SQL query and every report has specific values for SQL query parameters. To render a report I set parameters and call DataContext.ExecuteQuery method to get list of records. But to catch returned columns I have to know their names and have a...

C# - Resolving a parameter name at runtime

In C#, is there a way (terser the better) to resolve the name of a parameter at runtime? For example, in the following method, if you renamed the method parameter, you'd also have to remember to update the string literal passed to ArgumentNullException. public void Woof(object resource) { if (resource == null) {...

How can I get a value of a property from an anonymous type?

I have a datagrid populated by a Linq query. When the focused row in the datagrid changes I need to set a variable equal to one of the properties in that object. I tried... var selectedObject = view.GetRow(rowHandle); _selectedId = selectedObject.Id; ... but the compiler doesn't care for this at all ("Embedded statement cannot be a d...

Anonymous types

Hello, My first try to use anonymous types(test): private void button4_Click(object sender, EventArgs e) { test(new { a = "asd" }); } private void test(string a) { } I get an error "cannot convert from 'AnonymousType#1' to 'string' " Also I'd like to know how to pass an anonymous type if the paramete...

The evilness of 'var' in C#?

Possible Duplicates: C# 'var' keyword versus explicitly defined variables C# - Do you use "var"? This is a relatively simple question...more of a poll really. I am a HUGE fan of C#, and have used it for over 8 years, since before .NET was first released. I am a fan of all of the improvements made to the language, including lam...

Linq syntax in VB.NET

What I really want is to select these two tables in to an anon type like in Scott Gu's blog: here However, I would settle for this created type "ActiveLots" I am joining two tables together and want to be able to reference columns from each in my result set. I don't seem to be getting the syntax correctly. Dim pi = From p In dc.Inve...

how to use foreach in linq

I have read in some blog some time ago (sorry for being vague) that i could use a linq like the following var list = from c in xml select new { foreach(XElement el in c.Elements()) { } } Does anyone know is it possible or is it just my imagination?? Thanks. ...

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

in c#, var x = new {}; declares an anonymous type with no properties. Is this any different from var x = new object(); ? ...

What is the purpose of anonymous types?

What are the best use cases for anonymous types? It would seem to me that they are only useful within a limited context, for example one class or one function. They can hardly be used outside of this context, because without reflection no one will know what properties are available on these types. ...

Output from anonymous classes?

How can I get output from Java anonymous classes? In .Net I would use closures. executor = Executors.newSingleThreadExecutor(); final Runnable runnable = new Runnable() { public Exception exception; @Override public void run() { try { doSomething(); } catch (Exception exception) { // I'd like to report thi...

How to return anonymous type from c# method that uses LINQ to SQL

Possible Duplicate: LINQ to SQL: Return anonymous type? I have a standard LINQ to SQL query, which returns the data as an anonymous type (containing about 6 columns of data of various datatypes). I would like to make this returned object available to other parts of the program, either by returning it to the method-caller, or by...

Linq, VB - Anonymous type cannot be converted to anonymous type.

I'm a Linq noobie, maybe someone can point me in the right direction. What's wrong here? These anonymous types seem to have the same signatures. '*** Get all of the new list items' Dim dsNewFiles = From l1 In list1 _ Where Not (From l2 In list2 _ Select...

How to access property of anonymous type in C#?

I have this: List<object> nodes = new List<object>(); nodes.Add( new { Checked = false, depth = 1, id = "div_" + d.Id }); ... and I'm wondering if I can then grab the "Checked" property of the anonymous object. I'm not sure if this is even possible. Tried doing this: if (nodes.Any(n => n["Checked"] == false)) ......

Anonymous type in Repeater DataBound event

Hi All I'm setting the DataSource of an ASP.NET repeater as follows: rptTargets.DataSource = from t in DB.SalesTargets select new { t.Target, t.SalesRep.RepName }; Now, in the repeater's OnDataBound event, how can I retrieve the RepName and Target properties from the anonymous type contained in e.Item.DataItem? Many Thanks ...

Can I use Attributes with Anonymous classes?

I have a anonymous class: var someAnonymousClass = new { SomeInt = 25, SomeString = "Hello anonymous Classes!", SomeDate = DateTime.Now }; Is there anyway to attach Attributes to this class? Reflection, other? I was really hoping for something like this: var someAnonymousClass = new { [MyAttribute()] SomeInt = 2...