Reputation: 135
I am trying to convert stored Proc to LINQ using only LinqPad. I have following code in Linq
var myVar = (from st in Student
where st.name == "abcd"
select st).Take(10);
if (myVar.Any())
{
// some statements
}
else
{
myVar = (from emp in Employee
where emp.name == "abcd"
select emp).Take(10);
}
But it is giving following error at select line in else block
Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<AnonymousType#2>'
Can anyone know what is happening? How to solve it? How can I explicitly convert type in Linq?
Columns in both table are different. And I am not using any framework like entity framework etc. I just have the db of application and I am trying to convert all Stored Proc's into LINQ. And in the Stored Proc both values are caught in same variable and that's why I need them in same variable. So can it is possible in this scenario?
Upvotes: 1
Views: 146
Reputation: 125630
You queries returns different object, so you can't assign them to the same variable.
You could specify which properties you'd like to take from both queries and if they are the same it could work:
var myVar = (from st in Student
where st.name == "abcd"
select new
{
st.name,
st.age
}).Take(10);
if (myVar.Any())
{
// some statements
}
else
{
myVar = (from emp in Employee
where emp.name == "abcd"
select new
{
emp.name,
emp.age
}).Take(10);
}
Upvotes: 3