1

嗨,我想从 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

4 回答 4

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关键字之后添加了您的班级名称。

于 2013-10-22T22:54:43.317 回答
0

我在这里回答了类似的问题。您可以使用 SELECT 和 SELECTMANY https://stackoverflow.com/a/27755340/2764258简单地做到这一点

于 2015-01-03T13:45:00.417 回答
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();
于 2014-08-06T11:16:30.270 回答
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();
于 2014-08-07T09:46:34.787 回答