Return list with anonymous type in entity structure

How can I return a list with an anonymous type because with this code I get

"The type or namespace name 'T' cannot be found (are you missing the using directive or assembly references?)"

I only need to return IdMember and UserName, thanks

public static List<T> GetMembersItems(string ProjectGuid) { using (PMEntities context = new PMEntities("name=PMEntities")) { var items = context.Knowledge_Project_Members.Include("Knowledge_Project").Include("Profile_Information") .Where(p => p.Knowledge_Project.Guid == ProjectGuid) .Select(row => new { IdMember = row.IdMember, UserName = row.Profile_Information.UserName }); return items.ToList(); } } 
+6
source share
7 answers

As Jacob said, you cannot return an anonymous type, but you can create a new class and use it inside your select statement. Here is a similar question for Linq To Sql. The same method can be applied to entity infrastructure.

Unknown LinqToSql type

+7
source

The Tuple <> class is executed for such situations. Creating a custom class, as already suggested, is clearer, but Tupple is also doing its job.

eg.

 .Select(row => new Tuple<int,string>(row.IdMember,row.Profile_Information.UserName)) 

To access the properties of the element on the other side of the wire, you need to use:

 var id=t.Item1 var name=t.Item2 
+11
source

Since you are returning objects of an anonymous type, you cannot declare this in your return type for the method. Your attempt to use a common <T> will not work for this. There is no safe type method for declaring such a method. If you declare your return type as IList , then this should work, but you still will not have type safety.

You just have to declare a type.

Update:

Declaring a simple return type is not so bad. You can write a class like this:

 public class MemberItem { public string IdMember { get; set; } public string UserName { get; set; } } 

And then write your method as follows:

 public static List<MemberItem> GetMembersItems(string ProjectGuid) { using (PMEntities context = new PMEntities("name=PMEntities")) { var items = context.Knowledge_Project_Members.Include("Knowledge_Project").Include("Profile_Information") .Where(p => p.Knowledge_Project.Guid == ProjectGuid) .Select(row => new MemberItem { IdMember = row.IdMember, UserName = row.Profile_Information.UserName }); return items.ToList(); } } 
+5
source

The scope of anonymous types is limited by the method in which they are defined. In your case, it is better to declare a separate class with the corresponding properties and return a collection of instances of this class. For instance,

 public class UserDetail { public int Id{get;set;} public string UserName {get;set;} } public static List<UserDetail> GetMembersItems(string ProjectGuid) { using (PMEntities context = new PMEntities("name=PMEntities")) { var items = context.Knowledge_Project_Members.Include("Knowledge_Project").Include("Profile_Information") .Where(p => p.Knowledge_Project.Guid == ProjectGuid) .Select(row => new UserDetail{ IdMember = row.IdMember, UserName = row.Profile_Information.UserName }); return items.ToList(); } } 
+3
source

Just use and ArrayList instead

  public static ArrayList GetMembersItems(string ProjectGuid) { ArrayList items = new ArrayList(); items.AddRange(yourVariable .Where(p => p.Knowledge_Project.Guid == ProjectGuid) .ToList()); return items; } 
+1
source

You can return anonymous types by dropping their Object, but this is rarely useful. However, if you return from it, say, to the WebApi controller, as a fast (not so) dirty DTO, I think that everything is in order. This only works with JSON. XML will complain about a schema or something like that, but these days everyone uses JSON :)

 public static List<Object> GetMembersItems(string ProjectGuid) { using (PMEntities context = new PMEntities("name=PMEntities")) { var items = context.Knowledge_Project_Members.Include("Knowledge_Project").Include("Profile_Information") .Where(p => p.Knowledge_Project.Guid == ProjectGuid) .Select(row => new { IdMember = row.IdMember, UserName = row.Profile_Information.UserName }); return items .ToList() // this is only needed to make EF happy otherwise it complains about the cast .Cast<Object>() .ToList(); } } 
+1
source

Instead, you can return a type: NameValueCollection or KeyValuePair. An anonymous type cannot be a return type.

0
source

Source: https://habr.com/ru/post/894246/


All Articles