簡體   English   中英

為什么此linq投影不起作用?

[英]Why does this linq projection not work?

由於某些原因,注釋掉的版本不起作用? 我想做的是去掉UI中不需要的字段。這是正確的方法嗎?

   public IQueryable<tMember> Get(int id)
    {


        var congressGroup = from m in db.tMembers
                            join mp in db.tMemPositions on m.MembersID equals mp.MembersID
                            join cmp in db.tCongressMemPositions on mp.MemPositionsID equals cmp.MemPositionsID
                            join c in db.tCongresses on cmp.CongressID equals c.CongressID
                            where c.CongressNumber == id
                            //  select new tMember { Firstname = m.Firstname, Lastname = m.Lastname };
                            select m;

        //testing
        foreach (tMember m in congressGroup)
        {


        }


        return congressGroup;
    }

編輯1:兩種方式都可以編譯。 僅使用select m即可編譯並正常運行。 與另一行談到foreach時說

mscorlib.dll中發生類型'System.NotSupportedException'的異常,但未在用戶代碼中處理

附加信息:實體或復雜類型'CongressDb_DevModel.tMember'不能在LINQ to Entities查詢中構造。

EDIT2:構造函數是

   public tMember()
    {
        this.tMemMilitaries = new HashSet<tMemMilitary>();
        this.tMemOccupations = new HashSet<tMemOccupation>();
        this.tMemPositions = new HashSet<tMemPosition>();
        this.tMemRatings = new HashSet<tMemRating>();
        this.tMemVoteScores = new HashSet<tMemVoteScore>();
    }

只需創建一個新類型,而不要嘗試重用tMember

public class Name
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

然后更改方法的返回類型,並將選擇更改為:

select new Name { Firstname = m.Firstname, Lastname = m.Lastname };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM