基于Linq查询中的整数属性值为String属性赋值

时间:2014-12-02 05:57:30

标签: c# linq entity-framework linq-to-entities

我有linq query,并希望将字符串值分配给字符串属性,该属性取决于另一个int属性值。我在数据库中保存了int值,现在想要为保存的整数值获取适当的字符串值。以下是我的询问..

 var data = db.ProjectSetups.Select(c => new ProjectSetup
                {
                    ProjectId = c.ProjectId,
                    Name = c.Name,
                    NameArabic = c.NameArabic,
                    StartDate = c.StartDate,
                    EndDate = c.EndDate,
                    Date = c.Date,
                    StringType = (c.Type.ToInt32() == 1 ? "Development" : "Rental").ToString(),
                    StringStatus = (c.Status == 1 ? "InProgress" :
                    c.Status == 2 ? "Completed" :
                    c.Status == 3 ? "Dividend" : "Closed"),
                    LandArea = c.LandArea,
                    SaleAmount = c.SaleAmount

                }).ToList();

我收到了以下错误

实体或复杂类型' PMISModel.ProjectSetup'无法在LINQ to Entities查询中构建

1 个答案:

答案 0 :(得分:1)

 var data = db.ProjectSetups.ToList();
     data = data.Select(c => new ProjectSetup
                        {
                            ProjectId = c.ProjectId,
                            Name = c.Name,
                            NameArabic = c.NameArabic,
                            StartDate = c.StartDate,
                            EndDate = c.EndDate,
                            Date = c.Date,
                            StringType = (c.Type.ToInt32() == 1 ? "Development" : "Rental").ToString(),
                            StringStatus = (c.Status == 1 ? "InProgress" :
                            c.Status == 2 ? "Completed" :
                            c.Status == 3 ? "Dividend" : "Closed"),
                            LandArea = c.LandArea,
                            SaleAmount = c.SaleAmount

                        }).ToList();