How to create anonymous objects of type IQueryable using LINQ

I work in an ASP.NET MVC project where I created two LinqToSQL classes. I also created a repository class for models, and I implemented some methods, such as LIST, ADD, SAVE in this class, which serves the data controller. Now in one of the repository classes I pulled some data with LINQ joins like this.

private HerculesCompanyDataContext Company = new HerculesCompanyDataContext(); private HerculesMainDataContext MasterData = new HerculesMainDataContext(); public IQueryable TRFLIST() { var info = from trfTable in Company.TRFs join exusrTable in MasterData.ex_users on trfTable.P_ID equals exusrTable.EXUSER select new { trfTable.REQ_NO, trfTable.REQ_DATE, exusrTable.USER_NAME, exusrTable.USER_LNAME, trfTable.FROM_DT, trfTable.TO_DT, trfTable.DESTN, trfTable.TRAIN, trfTable.CAR, trfTable.AIRPLANE, trfTable.TAXI, trfTable.TPURPOSE, trfTable.STAT, trfTable.ROUTING }; return info; } 

Now, when I call this method from my controller, I can not get the list. I want to know, without creating a class of custom data models, how can I return an object of an anonymous type, such as IQueryable. And since this does not apply to any data model, how does this relate to this list in the view.

+4
source share
2 answers

You cannot return an "anonymous type" object. Please see this previous post that talks about this situation, where it seems that anonymous types will allow us to fool strong typing a bit, but they do not.

Yes, moving from flexible types (like DataTables) to strongly typed objects is a huge part of the solution strategy. With LINQ-To-SQL, you should be able to pretty easily return strongly typed object objects. Your repository methods should return specifically typed IQueryable interfaces, such as IQueryable<TRF> , and not just IQueryable . Or, the repository method may simply return a single TRF object object or a list of these objects.

Keep in mind that anonymous types and implicitly typed variables are two different things. An anonymous type is created for you by the compiler backstage. In this case, do not put your data in an anonymous type, as in the LINQ query example. Assuming your object is trfTable, try the following:

 public IQueryable<trfTable> TRFLIST() { var info = from trfTable in Company.TRFs join exusrTable in MasterData.ex_users on trfTable.P_ID equals exusrTable.EXUSER select new trfTable return info; } 

The advantage of this is that it requires a huge advantage of the IQueryable nature, since criteria added outside your repository will actually be added to the generated query. That's cool. However, there is some good debate about whether this is the best way to do this.

Note that info here is implicitly typed by the compiler, but not an anonymous type . This is an IQuerable<trfTable> . This is a key difference.

+3
source

You can sort the returned anonymous type, just not as strong as the typed one. I use this method to create json serializable types.

Returns the type of "object" in your data model class:

 public object TRFLIST() { var info = from trfTable in Company.TRFs join exusrTable in MasterData.ex_users on trfTable.P_ID equals exusrTable.EXUSER select new { trfTable.REQ_NO, trfTable.REQ_DATE, exusrTable.USER_NAME, exusrTable.USER_LNAME, trfTable.FROM_DT, <snipped> }; return info; } 


Then call it from your controller like this (in this example I use it to return JsonResult):

 public JsonResult() { var myAnonTRFLIST = dataClassInstance.TRFLIST(); return Json(myAnonTRFLIST, JsonRequestBehavior.Allowget); } 


You can also explore the use of the new dynamic type if you are in 4.0 ...


-UPDATE: I just tried this and it worked perfectly -

Returns the type of "object" in your data model class:

 public List<object> TRFLIST() { var info = from trfTable in Company.TRFs join exusrTable in MasterData.ex_users on trfTable.P_ID equals exusrTable.EXUSER select new { trfTable.REQ_NO, trfTable.REQ_DATE, exusrTable.USER_NAME, exusrTable.USER_LNAME, trfTable.FROM_DT, <snipped> }; return info.ToList<object>(); } public JsonResult() { dynamic myAnonTRFLIST = dataClassInstance.TRFLIST(); string test = myAnonTRFLIST[0].USER_NAME; <Do something with Test> return Json(myAnonTRFLIST, JsonRequestBehavior.Allowget); } 
+1
source

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


All Articles