C# LINQ to Entities - 如何从实体返回单个字符串值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13200658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ to Entities - How to return a single string value from entity
提问by BattlFrog
I am working in asp mvc 3 app. I have a model/entity called History. I have a linq query that returns one value. Depending on what I do, I either get a "Object not set to an instance" error in the controller when the method is called, or I get a "cannot implicitly convert from string to type Models.History." So I am looking for assistance in resolving, do I just need to cast it or something?
我在 asp mvc 3 应用程序中工作。我有一个名为 History 的模型/实体。我有一个返回一个值的 linq 查询。根据我所做的,我要么在调用该方法时在控制器中收到“对象未设置为实例”错误,要么收到“无法从字符串隐式转换为 Models.History 类型”。所以我正在寻求解决问题的帮助,我是否只需要投射它或其他什么?
Here is the method that gives the 'object not set' error:
这是给出“未设置对象”错误的方法:
public string GetPastAbuseData(int Id)
{
var query = (from h in _DB.History
where h.ApplicantId.Equals(Id)
select h.AbuseComment).FirstOrDefault();
return query.ToString();
}
Controller: vm.HistoryModel.AbuseComment = repo.GetPastAbuseData(Id);
控制器:vm.HistoryModel.AbuseComment = repo.GetPastAbuseData(Id);
And if I change the method type from string to History I get the 'cannot convert' error:
如果我将方法类型从字符串更改为历史记录,我会收到“无法转换”错误:
public History GetPastAbuseData(int Id)
{
return (from h in _DB.History
where h.ApplicantId.Equals(Id)
select h.AbuseComment).SingleOrDefault();
}
Thank you for your time.
感谢您的时间。
采纳答案by Sergey Berezovskiy
You are selecting AbuseCommentproperty (which is string) from HistoryObject. Thus your code tries to convert string to History. Just return whole Historyentity:
您正在AbuseComment从HistoryObject. 因此,您的代码尝试将字符串转换为History. 只需返回整个History实体:
public History GetPastAbuseData(int Id)
{
return (from h in _DB.History
where h.ApplicantId.Equals(Id)
select h).SingleOrDefault();
}
Also in first case querywill be of string type. You don't need to call ToStringon this variable. Even more, when you fall into OrDefault()case, you will have NullReferenceException.
同样在第一种情况下query将是字符串类型。您不需要调用ToString此变量。更重要的是,当您陷入困境时OrDefault(),您将拥有NullReferenceException.
public string GetPastAbuseData(int Id)
{
return (from h in _DB.History
where h.ApplicantId.Equals(Id)
select h.AbuseComment).FirstOrDefault();
}
回答by mojo722
Your first example is fine you just need to check for null.
您的第一个示例很好,您只需要检查 null。
public string GetPastAbuseData(int Id)
{
var query = (from h in _DB.History
where h.ApplicantId.Equals(Id)
select h.AbuseComment).FirstOrDefault();
return query == null ? string.empty : query;
}
回答by nerdybeardo
You can use the null coalescing operator which will check if null and return string.Empty if it is null. ?? Operator
您可以使用空合并运算符来检查是否为空,如果为空则返回 string.Empty。 ?? 操作员
public string GetPastAbuseData(int Id)
{
return _DB.History.FirstOrDefault(h=>h.ApplicantId.Equals(Id)).Select(h=>h.AbuseComment) ?? string.Empty;
}

