anonymous-types

Looking for a generic way to handle Linq reports & return results in XML

I'm designing a reporting engine for my project. The project will have several reports, each of which can be expressed as a Linq query; some will have parameters, while others won't. Now in order to meet my clients' requirements, I need to return the results of the queries in XML format, so that they can apply an XSL transform to make ...

Cloning Anonymous Types?

What I want to do is pass IEnumerable to a method and have it return a copy of IEnumerable. However, I want each in the collection to be a copy, rather than just the collection being a copy. An example: // some random data from L2S var data = (from p in sourceData select new { a = 1, ...

Are c# anonymous methods object oriented?

I'm just checking out anonymous methods (in c#)--part of me likes the flexibility and short-hand, but I'm also concerned that it may make the code harder to read. It also occurred to me that this construct seems to go against some of the o/o paradigm. Do you consider anonymous methods to be in-line with object oriented principles? ...

LINQ to SQL, anonymous types are read-only

Hi all, I'm using LINQ to SQL and DataGridViews to create an editable user interface of a database. My problem is that I have to translate the column names when fetching the results from the database, because while all names in the database are in English (the only language supported by the visual studio class designer) I have to displa...

Is there a way to return Anonymous Type from method?

I know I can't write a method like: public var MyMethod() { return new{ Property1 = "test", Property2="test"}; } I can do it otherwise: public object MyMethod() { return new{ Property1 = "test", Property2="test"} } but I don't want to do the second option because, if I do so, I will have to use reflection. Why I want to do...

Getting single result from a query with anonymous types in Linq To Sql

I have a query with single result. The result is an anonymouse type. How can I use access the type so I don't have to use query.Single() every time? This is my query: var result = from row in db.Table select new { CustomName = row.RowName }; This is how I use it right now: string name = result.Single().CustomName; Of course my re...

C++ creating and collecting structs in a loop

I want to create a struct from data harvested by line from a file. Each line necessitates a new struct, and the lines are accessed in a while loop. In C# I did this by creating anonymous structs and adding them to a list of structs. C++ would seem not to allow anonymous structs. I tried naming them with an incrementing variable, but this...

c#: Cast to Anonymous Type

Hi I had the following problem today, and i was wondering if there is a solution for my problem. My idea was to build anonymous classes and use it as a datasource for a WinForm Bindingsource: public void Init() { var option1 = new { Id = TemplateAction.Update, ...

Passing LINQ Results to a function

I have a class called UserInfo that contains details about a given user. There are several places in code where the data might be queried and I'd like to have a single function to fill the UserInfo object with the corresponding data from the Linq Query. var userData = dc.Users.Where(λ => (λ.Login == username) && λ.Activ...

LINQ Filter anonymous type based on IEnumerable values within type

I'm using LINQ to SQL like: var b = from s in context.data select new { id = s.id, name = s.name myEnumerable = s.OneToMany }; Where myEnumerable is of type IEnumberable<T> and I want to now get a subset of b based upon properties of the individual items of myEnumerable. For example, say <T> ...

What relevant differences are there between anonymous and predefined classes in Java?

I have a large tree-like data structure of objects which behave mostly identical but differ in one or two methods that calculate some keys used to navigate through the structure. The divergent behaviour depends on where the objects are in the structure. I was starting out with an abstract base class and have several subclasses that impl...

How does C# turn a variable name into an anonymous object property name?

When you create a new anonymous object using the following syntax: string name = "Foo"; var myObject = new { name }; You get an object with a property named 'name': myObject.name == "Foo"; //true What method does C# use to extract the variable name? ...

Creating a dynamic anonymous types vairables

Hello, I would like to ask if i can create a anonymous type variable and later on i can add more Properties? like var x = new { Name = "Ahmed" }; and want to add Age to it? how i can do this? Another question: i saw on some blogs a type AnonymousType what is the name space for this class? here is am example http://www.codeproject.com/KB...

C# Anonymous types problem

What is wrong with this code-snippet? class Program { static void Main(string[] args) { var obj = new { Name = "A", Price = 3.003 }; obj.Name = "asdasd"; obj.Price = 11.00; Console.WriteLine("Name = {0}\nPrice = {1}", obj.Name, obj.Price)...

WCF and Anonymous Types

I want to return an anonymous type over WCF. Is this possible? ...

How can i extract value of properties from anonymous class ?

When i declare object o = new { name = "Bruce",Age=21 }; Console.WriteLine("name={0},age={1}",???,??? ); Now how can i print value of name and age? ...

Why can't new[] {AnonymousType, AnonymousType} be casted to IEnumerable?

Hi, I have an array of an anonymous type declared as: var list = new[] { new {Name = "A", Age = 10}, new {Name = "B", Age = 15} } Now list inherits from type Array, which implements IEnumerable. Why does the following fail: Convert.ChangeType(list, typeof(IEnumerable)); This also fails: Convert.ChangeType(list, typeof(A...

A dictionary where value is an anonymous type in C#

Is it possible in C# 3.net to create a System.Collections.Generic.Dictionary<TKey, TValue> where TKey is unconditioned class and TValue - an anonymous class with a number of properties, for example - database column name and it's localized name. Something like this: new { ID = 1, Name = new { Column = "Dollar", Localized = "Доллар" } }...

How do I get values from SelectedItem in ComboBox with Linq and C# 3.5

I am really missing something with anonymous types, because I can't figure out what to do with the Combobox.SelectedItem property. Here's the code that populates the combobox, and it works just fine var stocks = from st in brdc.tb_dStocks join su in brdc.tb_rStockUsers on st.StockID equals su.StockID ...

Is there a Linq operation to determine whether there are items in a collection who have the same values for a pair of properties?

C#: I have a collection of objects . T has 2 properties. Property A and Property B. The rule that this collection needs to adhere to is that the combination of values for A and B must be unique within the collection. In other words, A and B need to serve as a composite primary key. Is there an operation in Linq I can use to check this c...