anonymous-types

How do I create and access a new instance of an Anonymous Class passed as a parameter in C#?

守給你的承諾、 提交于 2019-12-29 04:02:26
问题 I have created a function that takes a SQL command and produces output that can then be used to fill a List of class instances. The code works great. I've included a slightly simplified version without exception handling here just for reference - skip this code if you want to jump right the problem. If you have suggestions here, though, I'm all ears. public List<T> ReturnList<T>() where T : new() { List<T> fdList = new List<T>(); myCommand.CommandText = QueryString; SqlDataReader nwReader =

Why can't I pass an anonymous type as a parameter to a function?

浪子不回头ぞ 提交于 2019-12-23 12:52:38
问题 I was trying to do something like below but it doesn't work. Why won't .NET let me do this? private void MyFunction(var items) { //whatever } 回答1: Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of i are functionally equivalent: var i = 10; // implicitly typed int i = 10;

Using dynamic in C# to access field of anonymous type - possible?

早过忘川 提交于 2019-12-23 10:16:14
问题 I've got a controller method: public JsonResult CalculateStuff(int coolArg) { if(calculatePossible) return Json(CoolMethod(coolArg)); else return Json(new { Calculated = false }); } Now, I'd like to test this. public void MyTest { var controller = GetControllerInstance(); var result = controller.CalculateStuff().Data as dynamic; Assert.IsTrue(result.Calculated == false); } This throws a RuntimeBinderException saying that Calculated is not defined. Is there any way to achieve this? UPDATE

Final Local Variable may not have been initialized in anonymous inner class

限于喜欢 提交于 2019-12-23 08:50:20
问题 Here is my code: final Foo myFoo = new Foo(new Inner() { @Override callback(){ myFoo.bar(); } }); (With actual function names) final MyArrayAdapter aa = new MyArrayAdapter(new View.OnClickListener() { @Override onClick(){ aa.notifyDataSetChanged(); } }); Java is giving me an error about how myFoo might not have been initialized. Is there any way to fix this? I can potentially set the callback to null when I construct the object and then change it afterwards, but I'd hope for there to be a

EntityFramework anonymous composite key property name conflict

天涯浪子 提交于 2019-12-23 08:49:14
问题 I'm using EntityFramework 5 (or 4.3 for .Net Framework 4.0) In my DbContext object I have already set the correct DbSets and the objects contain proper references to each other. This is not new for me, and things are working well. Now in this case I have some composite keys that, sometimes, include the foreign key of a table (or object in this case). For this I use the HasKey<>() function on the OnModelCreating method of the DbContext. When these properties have different names, there is no

Can an anonymous type inherit from another type?

假装没事ソ 提交于 2019-12-23 07:51:33
问题 According to the MSDN documentation on the StringComparer.OrdinalIgnoreCase property: The OrdinalIgnoreCase property actually returns an instance of an anonymous class derived from the StringComparer class. Is this a feature I'm unfamiliar with—anonymous types with inheritance? Or by "anonymous class" did the author simply mean "internal class deriving from StringComparer , not visible to client code"? 回答1: If you look at the source code for StringComparer, you can see that OrginalIgnoreCase

LINQ How to select more than 1 property in a lambda expression?

若如初见. 提交于 2019-12-23 06:57:50
问题 We often use the following lambda expression MyList.Select(x => x.Id).ToList(); Is possible to get more than 1 property usinglambda expression ? E.g Id and Name from MyList? I know that I can use the following syntax: (from item in MyList select new { item.Id, item.Name }).ToList(); Can I do the same thing using lambda expression? 回答1: MyList.Select(x => new { x.Id, x.Name }).ToList(); 回答2: The feature you're interested in is C# 3's Anonymous Types You can create a new instance of an

Declaring a LIST variable of an anonymous type in C#

血红的双手。 提交于 2019-12-22 21:16:38
问题 I'm working with LINQ to SQL with ASP.NET 4 and C#. I've created a LINQ query that results in a new LIST (ie. ToList()). The returned LIST is of an ANONYMOUS type, because it's being created dynamically by the query. I want to be able to declare a variable at the page level in the code behind so I can use it in other functions and also on the front page using '<%= %>'. Visual Studio tells me that the result is of an anonymous type, but gives me the classes names, for example: {CLASS1, CLASS2}

Declaring a LIST variable of an anonymous type in C#

和自甴很熟 提交于 2019-12-22 21:15:53
问题 I'm working with LINQ to SQL with ASP.NET 4 and C#. I've created a LINQ query that results in a new LIST (ie. ToList()). The returned LIST is of an ANONYMOUS type, because it's being created dynamically by the query. I want to be able to declare a variable at the page level in the code behind so I can use it in other functions and also on the front page using '<%= %>'. Visual Studio tells me that the result is of an anonymous type, but gives me the classes names, for example: {CLASS1, CLASS2}

C#: Anonymous types and property names

白昼怎懂夜的黑 提交于 2019-12-22 10:27:38
问题 Is there any difference at all between this: dataContext.People.Select(ø => new { Name = ø.Name, }); and this: dataContext.People.Select(ø => new { ø.Name, }); ? 回答1: They are identical; if no name is specified (and the right-hand-side is a simple member-access) then the name of the existing member is assumed. The name is only necessary to: change the name to something else (for example Name = grp.Key ) to give a name to a non-member-access expression (for example Count = grp.Count() ) 回答2: