ASP.NET MVC中LINQ查询结果赋值实体属性类型转换错误求助
Hey there! Let's break down what's going on here and fix this step by step.
Root Cause of the Errors
Your core issue is a type mismatch:
- In your
Indexcontroller, you're usingToArray()to store astring[](array of strings) inSession["PostalAddress"]. - But your
TblSENDERSINFORMATIONentity'sPostalAddressproperty is a singlestringtype. Directly assigning an array to a single string will always throw that conversion error. - The
Split()error happens because when you pull the value from Session, it's typed as a genericArray(not a string), and arrays don't have aSplit()method.
Fix 1: Adjust the LINQ Query to Get a Single Value
Assuming FullName is unique (each name maps to one customer), you don't need an array at all. Use FirstOrDefault() to fetch a single postal address string instead of an array:
public ActionResult Index(string searchBy, string search) { Session["FullName"] = search; // Replace ToArray() with FirstOrDefault() to get a single string (or null if no match) var dbsenderPostal = (from a in db.TblCUSTOMER_PROFILE where a.FullName == search select a.PostalAddress).FirstOrDefault(); Session["PostalAddress"] = dbsenderPostal; if (searchBy == "XXX") { return View(db.TblCUSTOMER_PROFILE.Where(senders => senders.GeneratedCode == search || search == null).ToList()); } else { return View(db.TblCUSTOMER_PROFILE.Where(senders => senders.CustomerFullName.StartsWith(search) || search == null).ToList()); } }
FirstOrDefault() executes the query immediately and returns either the first matching postal address or null (if no customer with that name exists). Now your Session stores a single string instead of an array.
Fix 2: Update the MorePost Action
Now you can safely assign the Session value to your entity property. Add a type check to avoid potential null reference issues:
[HttpPost] public ActionResult MorePost(string Itemn) { TblSENDERSINFORMATION sentprint = new TblSENDERSINFORMATION(); // Use 'as string' to safely cast Session values sentprint.NameOfSender = Session["FullName"] as string; sentprint.PostalAddress = Session["PostalAddress"] as string; sentprint.Item = Itemn; // Don't forget to add your return logic here (e.g., return View(sentprint);) return View(); }
Why You Saw the DbQuery Error
That earlier error about DbQuery comes from LINQ's deferred execution: if you don't call methods like ToArray(), ToList(), or FirstOrDefault(), you're not actually fetching data from the database—you're just storing a query object. Directly assigning that query object to a string property will throw a type mismatch error.
Bonus: Handling Multiple Addresses (If Needed)
If FullName can map to multiple customers (and thus multiple postal addresses), you'll need to convert the array into a single string (e.g., comma-separated) to fit the PostalAddress property:
In the Index controller:
var dbsenderPostalArray = (from a in db.TblCUSTOMER_PROFILE where a.FullName == search select a.PostalAddress).ToArray(); // Convert array to a single comma-separated string Session["PostalAddress"] = string.Join(", ", dbsenderPostalArray);
Now you can also use Split(',') on the value later if you need to turn it back into an array.
内容的提问来源于stack exchange,提问作者Dexter

