查询错误:LINQ to Entities不识别方法'System.字符串ToString () & # 39;

本文关键字:ToString 字符串 System 错误 Entities to LINQ 识别 查询 方法 | 更新日期: 2023-09-27 18:12:30

我必须在编辑页面中选中复选框。为此,我有一个这样的查询:

var queryFI=(from u in _db.User where u.UserID==id where u.IsActive==1
                             select u);
            var join_queryFI=from r in queryFI join f in _db.Financial on r.FinancialID equals f.FinancialID
                             into c
                             from d in c.DefaultIfEmpty()
                             select new viewpartial
                             {
                               Text = d.FiName,
                               Value = d.FinancialID.ToString(),
                               Selected = d.FinancialID == r.FinancialIntermediaryID ? true : false                              
                             };
            ViewBag.IfcList = join_queryFI.ToList();

我得到一个错误:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression

请帮助

查询错误:LINQ to Entities不识别方法'System.字符串ToString () & # 39;

可以用SqlFunctions.StringConvert方法代替ToString

试一次,SqlFunctions.StringConvert(d.FinancialID)

您可以在执行查询后转换为字符串:

var queryFI = (from u in _db.User
                where u.UserID == id
                where u.IsActive == 1
                select u);
var join_queryFI = from r in queryFI
                    join f in _db.Financial on r.FinancialID equals f.FinancialID
                        into c
                    from d in c.DefaultIfEmpty()
                    select new viewpartial
                    {
                        Text = d.FiName,
                        Value = d.FinancialID,
                        Selected = d.FinancialID == r.FinancialIntermediaryID ? true : false
                    };
ViewBag.IfcList = join_queryFI.ToList().Select(x => new { Text = x.Text, Value = x.Value.ToString(), Selected = x.Selected }).ToList();