1

Hi I want to select 4 columns form DB then select to a List

public struct keyData
{

    public int pid;
    public int sid;
    public string proid;
    public string title;
}
Entities dbconn = new Entities();
List<keyData> temRes = (
    from viewData in dbconn.vw1
    join hData in dbconn.tableH
    on new { pid= (int)viewData.pid, proid= viewData.proid}
    equals new { pid= (int)hData .pid, proid= hData .proid}
    into joinSet
    from joinUnit in joinSet.DefaultIfEmpty()
    where joinUnit == null
    select new { pid= (int)viewData.pid, sid= (int)viewData.sid, proid= viewData.proid, Title=viewData.Title }
    ).ToList();

then it says:

Error 3 Cannot implicitly convert

type 'System.Collections.Generic.List<AnonymousType#1>' 

to 'System.Collections.Generic.List<hl.Program.keyData>'

thanks

using MarcinJuraszek 's idea

it gives me :

Only parameterless constructors and initializers are supported in LINQ to Entities

4 Answers 4

5

That's because your quere return an anonymous type objects. Change it to return keyData instances instead:

    List<keyData> temRes = (
        from viewData in dbconn.vw1
        join hData in dbconn.tableH
        on new { pid= (int)viewData.pid, proid= viewData.proid}
        equals new { pid= (int)hData .pid, proid= hData .proid}
        into joinSet
        from joinUnit in joinSet.DefaultIfEmpty()
        where joinUnit == null
        select new keyData() { pid= (int)viewData.pid, sid= (int)viewData.sid, proid= viewData.proid, Title=viewData.Title }
        ).ToList();

The difference is in select clause. I've added your class name after new keyword.

Sign up to request clarification or add additional context in comments.

3 Comments

then it give me : Only parameterless constructors and initializers are supported in LINQ to Entities.
Do you have parameterless constructor defined for keyData class?
@BennyAe try adding an empty parameterless constructor to your keyData structure.
0
Entities dbconn = new Entities();
List<keyData> temRes = (
    from viewData in dbconn.vw1.ToList()
    join hData in dbconn.tableH.ToList()
    on new { pid= (int)viewData.pid, proid= viewData.proid}
    equals new { pid= (int)hData .pid, proid= hData .proid}
    into joinSet
    from joinUnit in joinSet.DefaultIfEmpty()
    where joinUnit == null
    select new { pid= (int)viewData.pid, sid= (int)viewData.sid, proid= viewData.proid,  Title=viewData.Title }
    ).ToList();

Comments

0

As it was said you must return keyData instances instead of anonimous type https://stackoverflow.com/a/19529805/1034373. But struct can't have an explicit parameterless constructor so you must either change keyData type to class or add a counstructor

public struct keyData
{
    public keyData(int pid, int sid, string proid, string title)
    {
         this.pid = pid;
         this.sid = sid;
         this.proid = proid;
         this.title = title;
    }
    public int pid;
    public int sid;
    public string proid;
    public string title;
}

and write code like this

List<keyData> temRes = (
    from viewData in dbconn.vw1
    join hData in dbconn.tableH
    on new { pid= (int)viewData.pid, proid= viewData.proid}
    equals new { pid= (int)hData .pid, proid= hData .proid}
    into joinSet
    from joinUnit in joinSet.DefaultIfEmpty()
    where joinUnit == null
    select new { pid= (int)viewData.pid, sid= (int)viewData.sid, proid= viewData.proid, Title=viewData.Title }
    ).ToList().Select(x => new keyData(x.pid, x.sid, x.proid, x.title)).ToList();

Comments

0

I have answered similar question here.You can simply do this using SELECT and SELECTMANY https://stackoverflow.com/a/27755340/2764258

1 Comment

This is a comment, not an answer. The normal procedure would be cast a close vote (duplicate) but I think you haven't got enough points for that yet. By the way, your answer to the other question does not apply here.

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.