Mizbella
Mizbella

Reputation: 946

Error in query:LINQ to Entities does not recognize the method 'System.String ToString()' method

I have to get checkboxes selected in edit page.For that i have a query like this:

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

I got an error:

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

Pls help

Upvotes: 1

Views: 341

Answers (2)

nima
nima

Reputation: 6733

You could convert to string after executing the query:

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

Upvotes: 1

Satpal
Satpal

Reputation: 133403

You can use SqlFunctions.StringConvert method instead of ToString

Try this once, SqlFunctions.StringConvert(d.FinancialID)

Upvotes: 1

Related Questions