こんばんは全員にLINQクエリの結果を変換します。は、カスタムモデルのリスト
私は、SQLクエリの結果にLINQを持っている、と私はここ
ダウンカスタムモデルクラスのリストにクラスモデルこの結果を変換したい:
public class Beneficiary
{
public int BeneficiaryId { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public DateTime? CreatedAt { get; set; }
[Column(TypeName = "date")]
public DateTime? BithDate { get; set; }
public int? CommunityId { get; set; }
public virtual Community Community { get; set; }
[Required]
public string Gender { get; set; }
public string IdNumber { get; set; }
[Required]
[StringLength(50)]
public string AppName { get; set; }
[StringLength(50)]
public string BeneficiaryNumber { get; set; }
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
public virtual ICollection<BeneficiaryProject> BeneficiaryProjects { get; set; }
public virtual ICollection<Card> Cards { get; set; }
public virtual ICollection<Invoice> Invoices { get; set; }
}
、ここでクエリがあること上記のクエリが正常に実行される
var list = (from B in db.Beneficiaries
join ben in db.BeneficiaryProjects on B.BeneficiaryId equals ben.BeneficiaryId
where (ben.CardNeeded == true && ben.ProjectId == temp.ProjectId)
select new Beneficiary()
{
BeneficiaryId = B.BeneficiaryId,
FirstName = B.FirstName,
LastName = B.LastName,
IdNumber = B.IdNumber,
Gender = B.Gender
});
が、私は以下のようBeneficiary
のリストにvar list
を変換する場合:私が使用
List<Beneficiary> list1 = list.ToList<Beneficiary>();
が、私は以下の例外だ:EFから結果を取得した後、カスタムクラスに
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: The entity or complex type 'E_Voucher.Models.Beneficiary' cannot be constructed in a LINQ to Entities query.
あなたはこれを試しましたか: 'List BeneficiaryList =(あなたのクエリはここにあります).ToList();' –
'ToList'を呼び出すまでクエリ自体は実行されないので、正常に実行されるかもしれません。実際にはそうではありません。 –
クエリを見て、temp.ProjectIDに問題がある可能性があります。あなたは参加していませんか? – DaniDev