C#Linq SQL选择多列到列表

时间:2013-10-22 22:52:52

标签: c# sql linq list

您好我想从DB中选择4列然后选择列表

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();

然后它说:

错误3无法隐式转换

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

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

感谢

使用MarcinJuraszek的想法

它给了我:

Only parameterless constructors and initializers are supported in LINQ to Entities

4 个答案:

答案 0 :(得分:5)

那是因为你的quere返回一个匿名类型的对象。将其更改为返回keyData个实例:

    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();

区别在于select子句。我在new关键字后添加了您的班级名称。

答案 1 :(得分: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();

答案 2 :(得分:0)

正如所说的那样,你必须返回keyData个实例而不是一个类型https://stackoverflow.com/a/19529805/1034373。但是struct不能有一个显式的无参数构造函数,因此你必须将keyData类型更改为class或添加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;
}

并编写这样的代码

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();

答案 3 :(得分:0)

我在这里回答了类似的问题。您只需使用SELECT和SELECTMANY即可 https://stackoverflow.com/a/27755340/2764258