Answered by:
Exeception User-Unhandled - Not Supported in LINQ to Entities.

Question
-
User-609870334 posted
Hi - I receive the following error -
System.NotSupportedException: 'The specified type member 'StockRecord' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.'
I have two SQL tables, the Shareholder table which is associated by a FK to the StockRecord table. My objective is to select items from the Shareholder table where the field PropertyPrime contains info in the "search" variable. The field PropertyPrime is a column in the second table called Stockholder.
My Shareholder table has a model which includes -
public partial class Shareholder
{
public int ShareholderID { get; set; }
public int Certificate { get; set; } //FK to StockRecord Table PK
...Other columns....
public string Notes { get; set; }public virtual string PropertyPrime { get; set; }
public virtual StockRecord StockRecord { get; set; }
}The controller code is as follows and I receive the error where yellow highlighted -
public ActionResult Index(string searchBy, string search, string includeItems, int? page)
{var shareholders = db.Shareholders.Include(s => s.StockRecord.PropertyPrime);
if (search == null) { search = " "; }
if (searchBy == "PropertyPrime" && includeItems != "B")
{
return View(db.Shareholders.Where(s => s.StockRecord.PropertyPrime.Contains(search) && s.Status == includeItems || search == null).ToList().ToPagedList(page ?? 1, 10));
}
else if (searchBy == "PropertyPrime" && includeItems == "B")
{
return View(db.Shareholders.Where(s => s.StockRecord.PropertyPrime.Contains(search) || search == null).ToList().ToPagedList(page ?? 1, 10));
}
else if (searchBy == "CertificateTitle" && includeItems != "B")
{return View(db.Shareholders.Where(s => s.LastName.Contains(search) && s.Status == includeItems || search == null).ToList().ToPagedList(page ?? 1, 10));
}else _ = (searchBy == "CertificateTitle" && includeItems == "B");
{
return View(db.Shareholders.Where(s => s.LastName.Contains(search) || search == null).ToList().ToPagedList(page ?? 1, 10));
}
//
}Many Thanks, Ken
Sunday, April 5, 2020 8:50 PM
Answers
-
User-17257777 posted
Hi ikeni,
return View(db.Shareholders.Where(s => s.StockRecord.PropertyPrime.Contains(search) && s.Status == includeItems || search == null).ToList().ToPagedList(page ?? 1, 10));
}You seem want to query through the properties of associated objects, if so, you should use .include() method first load the associated object.
For more details, you can refer to
https://docs.microsoft.com/en-us/ef/ef6/querying/related-data
https://www.entityframeworktutorial.net/eager-loading-in-entity-framework.aspx
Best Regards,
Jiadong Meng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 6, 2020 1:56 AM
All replies
-
User1120430333 posted
Hi - I receive the following error -
System.NotSupportedException: 'The specified type member 'StockRecord' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.'
I have two SQL tables, the Shareholder table which is associated by a FK to the StockRecord table. My objective is to select items from the Shareholder table where the field PropertyPrime contains info in the "search" variable. The field PropertyPrime is a column in the second table called Stockholder.
My Shareholder table has a model which includes -
public partial class Shareholder
{
public int ShareholderID { get; set; }
public int Certificate { get; set; } //FK to StockRecord Table PK
...Other columns....
public string Notes { get; set; }public virtual string PropertyPrime { get; set; }
public virtual StockRecord StockRecord { get; set; }
}The controller code is as follows and I receive the error where yellow highlighted -
public ActionResult Index(string searchBy, string search, string includeItems, int? page)
{var shareholders = db.Shareholders.Include(s => s.StockRecord.PropertyPrime);
if (search == null) { search = " "; }
if (searchBy == "PropertyPrime" && includeItems != "B")
{
return View(db.Shareholders.Where(s => s.StockRecord.PropertyPrime.Contains(search) && s.Status == includeItems || search == null).ToList().ToPagedList(page ?? 1, 10));
}
else if (searchBy == "PropertyPrime" && includeItems == "B")
{
return View(db.Shareholders.Where(s => s.StockRecord.PropertyPrime.Contains(search) || search == null).ToList().ToPagedList(page ?? 1, 10));
}
else if (searchBy == "CertificateTitle" && includeItems != "B")
{return View(db.Shareholders.Where(s => s.LastName.Contains(search) && s.Status == includeItems || search == null).ToList().ToPagedList(page ?? 1, 10));
}else _ = (searchBy == "CertificateTitle" && includeItems == "B");
{
return View(db.Shareholders.Where(s => s.LastName.Contains(search) || search == null).ToList().ToPagedList(page ?? 1, 10));
}
//
}Many Thanks, Ken
The message is clear. You cannot use StockRecord in the manner you're trying to use it in Linq-2-Entities. I don't even know what you're trying to do.
Sunday, April 5, 2020 9:54 PM -
User-17257777 posted
Hi ikeni,
return View(db.Shareholders.Where(s => s.StockRecord.PropertyPrime.Contains(search) && s.Status == includeItems || search == null).ToList().ToPagedList(page ?? 1, 10));
}You seem want to query through the properties of associated objects, if so, you should use .include() method first load the associated object.
For more details, you can refer to
https://docs.microsoft.com/en-us/ef/ef6/querying/related-data
https://www.entityframeworktutorial.net/eager-loading-in-entity-framework.aspx
Best Regards,
Jiadong Meng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 6, 2020 1:56 AM -
User-609870334 posted
Thank You!
Thursday, April 30, 2020 4:09 PM