linq to entities将连接数据分配给viewmodel

时间:2012-07-13 21:00:40

标签: asp.net-mvc-3 linq-to-entities entity-framework-4.1

这是一个linq查询,用于将数据分配给viewmodel。它很棒。

var data = (from C in db.CaseComplaints
                    where C.CasesID == caseid
                    select C.ComplaintCode).ToList().Select(x => new CaseComplaintsViewModel()
                        {
                            ComplaintCode = x.ComplaintCodeName,
                            ComplaintType = x.ComplaintType
                        }).ToList();

以下是尝试执行连接并将数据分配给viewmodel的代码。这是行不通的。代码编辑器告诉我x没有BranchName的方法或定义

var data = (from branch in db.Branches
                    join customer in db.Customers 
                    on branch.BranchID equals customer.BranchID
                    where customer.BranchID == bid
                    select branch.BranchName).ToList().Select(x => new CaseResponsibleBranchViewModel()
                        {
                            BranchName = x.BranchName
                        });

我错过了什么?

1 个答案:

答案 0 :(得分:2)

您已经选择了BranchName,我认为它是一个字符串,所以没有BranchName属性x:

var data = (from branch in db.Branches
                    join customer in db.Customers 
                    on branch.BranchID equals customer.BranchID
                    where customer.BranchID == bid
                    select branch.BranchName).ToList().Select(x => new CaseResponsibleBranchViewModel()
                        {
                            BranchName = x
                        });