如何使用实体框架执行IsNull? [英] How do I do an IsNull with entity framework?
问题描述
我有一个显示单个记录的结果页.
问题:
如果没有电话分机,如何排除(抑制)电话分机本身和分机".返回数据中的标签?
如果有扩展名,我想要:
•克里斯·康普顿919-754-6000转. 6512 chris@notmymail.net
其他
•克里斯·康普顿919-754-6512 chris@notmymail.net
如果本周没有答案,我将其编码为t-sql存储的proc,如下所示:
I have a results page that displays a single record.
Question:
If there isn''t a phone extension, how do I exclude (suppress) the phone extension itself and "ext." label from the returned data?
If there is an extension I want:
• Chris Compton 919-754-6000 ext. 6512 chris@notmymail.net
else
• Chris Compton 919-754-6512 chris@notmymail.net
If I don''t get an answer this week I''ll just code it into a t-sql stored proc like this:
SELECT ..., Phone, IsNull(''ext. ''+nullif([PhoneExt],''''), ''''), Email ...
(但我宁愿将该逻辑保留在数据库之外)
这是我所能(简化)的,但标签为"ext".始终显示:
(but I''d rather keep that logic out of the database)
Here''s what I have (shortened) that works, but the label "ext." always shows:
try { pk = Convert.ToInt32(Request.QueryString["id"]); }
catch (Exception) { pk = 0; }
var query = (
from c in context.tEmployees
where (c.pkEmployee == pk)
orderby c.NameLast, c.NameFirst, c.NameMiddle, c.Agency
select new
{ Name = c.First + " " + c.Last,
Telephone = c.Phone ,
TelephoneExt = " ext. " + c.PhoneExt,
Email = c.Email }
);
if (query.Count() == 1)
{ ResultsRepeater.DataSource = query;
ResultsRepeater.DataBind();
}
else
{ Response.Write("I'm sorry we could not find that employee, please try the search page again."); }
另外,如果有更好的方法来处理变量"pk",我欢迎提出建议.
注意:t-sql和代码都可以工作.如果我引入了错误,请缩短代码以向我道歉.
编辑6/7/12:
以下所有三种解决方案都很棒,对我有帮助!
在昨天结束之前,我及时看到了第一个答案及时部署工作代码(在质量检查中).
我实际上是今天早上换到第二个(我尽量避免在if子句中使用"not").
阅读第3条(和评论)后,我做了一些改进.
再次感谢!
-Chris C. 12/5/7下午EDT
Also, if there''s a better way to handle the variable "pk" I''m open to suggestions.
NOTE: that the t-sql and the code both work. If I''ve introduced an error shortening the code to post the question my apologies.
Edit 6/7/12:
ALL THREE solutions below are great and helped me!
I saw the first answer in time to deploy the working code (in QA) just before the end of my day yesterday.
I actually changed to the second one this morning (I try to avoid using "not" in an if clause).
I made a couple more improvements after reading number three (and the comments).
Thanks again!
-Chris C. 5/7/12 1 PM EDT
推荐答案
有关:
TelephoneExt = string.IsNullOrEmpty(c.PhoneExt) ? " ext. " + c.PhoneExt : string.Empty;
Clifford为IsNull问题提供了正确的解决方案,但我认为我想添加一种解析整数的替代方法:
Clifford has the correct solution for the IsNull issue, but I thought I''d add an alternative way to parse the integer:
if (!Int32.TryParse(Request.QueryString["id"], out pk))
{
// The TryParse will default it to 0 on failure,
// but I put this here in case you want to default to something else.
pk = 0;
}
var query = (
from c in context.tEmployees
where (c.pkEmployee == pk)
orderby c.NameLast, c.NameFirst, c.NameMiddle, c.Agency
select new
{ Name = c.First + " " + c.Last,
Telephone = c.Phone ,
TelephoneExt = c.PhoneExt != null ? " ext. " + c.PhoneExt : string.Empty,
Email = c.Email }
);
或
or
var query = (
from c in context.tEmployees
where (c.pkEmployee == pk)
orderby c.NameLast, c.NameFirst, c.NameMiddle, c.Agency
select new
{ Name = c.First + " " + c.Last,
Telephone = c.Phone ,
TelephoneExt = ! string.IsNullOrEmpty(c.PhoneExt) ? " ext. " + .PhoneExt : string.Empty,
Email = c.Email }
);
这篇关于如何使用实体框架执行IsNull?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!