0

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?)

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.