Linq to sql 转换可为null的二进制格式时的Linq到Sql问题
我正在尝试从DBSQLServer2008获取图像,该图像使用LINQtoSQL查询保存为二进制可空值。 我的问题是:Linq to sql 转换可为null的二进制格式时的Linq到Sql问题,linq-to-sql,Linq To Sql,我正在尝试从DBSQLServer2008获取图像,该图像使用LINQtoSQL查询保存为二进制可空值。 我的问题是: var news = from topic in db.PublicNews orderby topic.PostedDate descending select new PublicNewsDto() { Id = topic.Id, PostedDate = topic.PostedDate, S
var news = from topic in db.PublicNews
orderby topic.PostedDate descending
select new PublicNewsDto()
{
Id = topic.Id,
PostedDate = topic.PostedDate,
Summary = topic.Summary,
Text = topic.Text,
NewsImg = topic.NewsImg.ToArray()
};
我有空参考选项。若数据库不包含空值,那个么代码可以正常工作
你能帮帮我吗
谢谢 分配topic.NewsImg之前,必须先测试它是否有值。如果没有值,则传入空数组,如下所示:
NewsImg = topic.NewsImg.HasValue ? topic.NewsImg.Value.ToArray() : new byte[]
编辑:
清洁的方法如下:
var news = from topic in db.PublicNews
select new PublicNewsDto()
{
Id = topic.Id,
PostedDate = topic.PostedDate,
Summary = topic.Summary,
Text = topic.Text,
NewsImg = (topic.NewsImg.HasValue ? topic.NewsImg.Value : new byte[])
};
var orderedNews = news.OrderByDescending(e => e.PostedDate);
非常感谢您的回答,但我已经尝试了相同的代码,但它不起作用。我遇到了下一个错误:无法将表达式“TablePublicNew.OrderByDescendingtopic=>topic.PostedDate.Selecttopic=>new PublicNewsDto{Id=..…InvokevalueSystem.Func`1[System.Byte[]}”转换为SQL,无法将其作为本地表达式处理。并从表达式中删除“orderby topic.PostedDate descending”。如果要创建列表,则对行进行排序是没有意义的。如果你想让你的列表被排序,那就在最后排序。当然。结构是下一个:public类PublicNewsDto{public int Id;public DateTimeOffset postedate;public string Text;public string Summary;public byte[]NewsImg;}好的,这是我的查询。var news=from db.PublicNews中的topic选择new PublicNewsDto{Id=topic.Id,PostedDate=topic.PostedDate,Summary=topic.Summary,Text=topic.Text,NewsImg=topic.NewsImg!=null?topic.NewsImg.ToArray:new byte[]{0};