将LINQ查询的结果转换为自定义模型列表

时间:2017-01-04 17:14:31

标签: c# sql asp.net linq model-view-controller

大家晚上好。

我有一个LINQ to SQL查询结果,我想将此结果转换为Custom Model类列表

这里的班级模型:

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

上述查询已成功执行,但当我将var list转换为Beneficiary列表时,如下所示:

            List<Beneficiary> list1 = list.ToList<Beneficiary>();

但我得到以下例外:

 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.

3 个答案:

答案 0 :(得分:2)

从EF中检索结果后转换为自定义类:

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 {
        B.BeneficiaryId,
        B.FirstName,
        B.LastName,
        B.IdNumber,
        B.Gender
    }
).AsEnumerable()
.Select(B => new Beneficiary() {
    BeneficiaryId = B.BeneficiaryId,
    FirstName = B.FirstName,
    LastName = B.LastName,
    IdNumber = B.IdNumber,
    Gender = B.Gender
}).ToList();

注意:运行ToList时出错的原因是EF查询推迟执行。您的查询可能不正确,但找到答案的唯一方法是尝试获取结果。

答案 1 :(得分:1)

您收到此错误的原因 (在这一行:)

List<Beneficiary> list1 = list.ToList<Beneficiary>();

是您尝试将Linq Query变量转换为List。

您真正需要做的就是:

List<Beneficiary> list1 = list.ToList();

您对temp.ProjectID的查询:

var projects = db.Projects.Where(p => p.ContractId == id); foreach (var temp in projects)

应该是这样的:

int ProjectId = (from P in db.Projects where P.ContractId == id select P.ProjectId).FirstOrDefault();

然后获取该值并输入您的下一个查询:

... where (ben.CardNeeded == true && ben.ProjectId == ProjectId)

答案 2 :(得分:0)

我相信你不能将LINQ to Entities中的Beneficiary类型的对象投影到映射的实体对象。

试试这个:

db.Beneficiaries.Join(db.BeneficiaryProjects, B => B.BeneficiaryId, ben => ben.BeneficiaryId, (B, ben) => new { B, ben })
                .Where(x => x.ben.CardNeeded == true && x.ben.ProjectId == temp.ProjectId)
                .Select(x => x.B)
                .ToList();

这是一个lamda表达式,应该与LINQ查询做同样的事情,但会选择您的Beneficiary模型对象并映射它。 ToList()可以根据您的需要替换为AsQueryable()AsEnumerable()