Reputation: 1
Please read my question carefully and reply me.
I have two tables as table1 and table2.
In table1 I have columns as AddressID(Primary Key),Address1,Address2,City
In table2 I have columns as ContactID(Primary Key),AddressID(Foriegn Key),Last Name,First Name.
By using join operation I can retrieve data from both the tables.
I created a Model in my MVC Application. I can see both the tables in entity editor.
In the ViewData folder of my solution explorer I have created two class as ContactViewData.cs and SLXRepository.cs.
In the ContactViewData.cs, I have following code
public IEnumerable<CONTACT> contacts
{
get;
set;
}
In the SLXRepository.cs, I have following code
public IEnumerable<CONTACT> GetContacts()
{
var contact =
(
from c in context.CONTACT
join a in context.ADDRESS on c.ADDRESSID equals a.ADDRESSID
select new
{
a.ADDRESS1,
a.ADDRESS2,
a.CITY,
c.FIRSTNAME,
c.LASTNAME
}
);
return contact;
}
I am getting the error in return type
Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<SiennaSLX.Models.CONTACT>'. An explicit conversion exists (are you missing a cast?)
Upvotes: 0
Views: 2335
Reputation: 458
In Linq2Sql, your particular query will return an IQueryable object.
You can either change the return type of your method to IQueryable
public IQueryable<CONTACT> contacts
{
get;
set;
}
public IQueryable<CONTACT> GetContacts()
{
var contact =
(
from c in context.CONTACT
join a in context.ADDRESS on c.ADDRESSID equals a.ADDRESSID
select new
{
a.ADDRESS1,
a.ADDRESS2,
a.CITY,
c.FIRSTNAME,
c.LASTNAME
}
);
return contact;
}
or you can cast your return value with the .AsEnumerable() function:
public IEnumerable<CONTACT> GetContacts()
{
var contact =
(
from c in context.CONTACT
join a in context.ADDRESS on c.ADDRESSID equals a.ADDRESSID
select new
{
a.ADDRESS1,
a.ADDRESS2,
a.CITY,
c.FIRSTNAME,
c.LASTNAME
}
);
return contact.AsEnumerable();
}
Upvotes: 1