Asked by:
error while querying sql through linq

Question
-
User81789783 posted
i m facing below error
Error 7 Cannot implicitly convert type 'System.Linq.IQueryable<CallCenterCRM.TBL_Department>' to 'System.Collections.Generic.List<CallCenterCRM.Models.TBL_Department>'. An explicit conversion exists (are you missing a cast?) C:\Users\erum\Documents\Visual Studio 2012\Projects\CallCenterCRM\CallCenterCRM\Models\TBL_Department.cs 134 29 CallCenterCRM
i have below codepublic static List<TBL_Department> GetDeptbyprefix(string prefix) { CallCenterDboDataContext obj = new CallCenterDboDataContext("Data Source=erum-pc;Initial Catalog=CallCenter;User ID=sa;Password=sa;"); List<TBL_Department> DeptList = new List<TBL_Department>(); //DeptList = from dept in obj.TBL_Departments // where dept.prefix == prefix // select dept; using (CallCenterDboDataContext dbcontext = new CallCenterDboDataContext()) { DeptList = from dept in dbcontext.TBL_Departments where dept.prefix == prefix select dept; } return DeptList; }
Friday, November 24, 2017 4:42 AM
All replies
-
User1120430333 posted
DeptList = (from dept in dbcontext.TBL_Departments where dept.prefix == prefix select dept).ToList();
https://msdn.microsoft.com/en-us/library/bb342261%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Creates a List<T> from an IEnumerable<T>.
https://msdn.microsoft.com/en-us/library/bb351562(v=vs.110).aspx
This interface inherits the IEnumerable<T> interface so that if it represents a query, the results of that query can be enumerated. Enumeration forces the expression tree associated with an IQueryable<T> object to be executed. Queries that do not return enumerable results are executed when the Execute<TResult>(Expression) method is called.
Friday, November 24, 2017 5:41 AM -
User991499041 posted
Hi Erum,
If you read the error message. It's telling you EXACTLY what's wrong.
To convert IQuerable to a list, you can do the following:
var q = from dept in dbcontext.TBL_Departments where dept.prefix == prefix select dept; DeptList = q.ToList();
Regards,
zxj
Friday, November 24, 2017 5:42 AM -
User81789783 posted
well zxj i changed code as per ur suggestion but same issue ,but one error this time
Error 7 Cannot implicitly convert type 'System.Collections.Generic.List<CallCenterCRM.TBL_Department>' to 'System.Collections.Generic.List<CallCenterCRM.Models.TBL_Department>' C:\Users\erum\Documents\Visual Studio 2012\Projects\CallCenterCRM\CallCenterCRM\Models\TBL_Department.cs 141 24 CallCenterCRM
public static List<TBL_Department> GetDeptbyprefix(string prefix) { CallCenterDboDataContext obj = new CallCenterDboDataContext("Data Source=erum-pc;Initial Catalog=CallCenter;User ID=sa;Password=sa;"); List<TBL_Department> DeptList = new List<TBL_Department>(); //DeptList = from dept in obj.TBL_Departments // where dept.prefix == prefix // select dept; var q = from dept in obj.TBL_Departments where dept.prefix == prefix select dept; DeptList = q.ToList(); return DeptList; }
Friday, November 24, 2017 5:50 AM -
User81789783 posted
DA924 ,ur lines of code is not working at all ,
Friday, November 24, 2017 5:51 AM -
User1120430333 posted
Error 7 Cannot implicitly convert type 'System.Collections.Generic.List<CallCenterCRM.TBL_Department>' to 'System.Collections.Generic.List<CallCenterCRM.Models.TBL_Department>' C:\Users\erum\Documents\Visual Studio 2012\Projects\CallCenterCRM\CallCenterCRM\Models\TBL_Department.cs 141 24 CallCenterCRM
You can't do it because you can't cast an object across to different namespace even if its the same object.
CallCenterCRM.Models.TBL_Department vs CallCenterCRM.TBL_Department are two objects that may be the same object but they are not as .NET sees it.
http://csharp-station.com/Tutorial/Linq/Lesson02
Listing 2-5: Custom Projection with Data Source Type
TBL_Department is the projected object you will make using the custom projection lorded into your List<TBL_Department>
And put the ().Tolist() around the code as you were shown.
Friday, November 24, 2017 6:43 AM