无法将类型'System.Linq.IQueryable 转换为byte [duplicate](Cannot convert type 'System.Linq.IQueryab

编程入门 行业动态 更新时间:2024-10-13 04:26:04
无法将类型'System.Linq.IQueryable 转换为byte [duplicate](Cannot convert type 'System.Linq.IQueryable to byte [duplicate])

这个问题在这里已有答案:

将system.data.linq.binary转换为byte [] 4个答案

我正在尝试通过linq从我的数据库中选择一个image列并将其保存到q3

------------------------------- | table : admin | ------------------------------- | username | password | image | -------------------------------

查询:

var q3 = alnq.tbl_Admins.Where(c => c.fld_UserName == userName).Select(c => c.fld_Image);

当我想将此查询转换为字节数组时,我收到一个错误:

byte [] DbImageByte = q3.ToArray();

错误:

无法将类型'System.Data.Linq.Binary []'隐式转换为'byte []'

This question already has an answer here:

convert system.data.linq.binary to byte[] 4 answers

I'm trying to select an image column from my database via linq and save it into q3

------------------------------- | table : admin | ------------------------------- | username | password | image | -------------------------------

Query:

var q3 = alnq.tbl_Admins.Where(c => c.fld_UserName == userName).Select(c => c.fld_Image);

When I want to convert this query to an array of bytes, I get an error:

byte [] DbImageByte = q3.ToArray();

Error:

Cannot implicitly convert type 'System.Data.Linq.Binary[]' to 'byte[]'

最满意答案

在Linq

每当我们写select选择where条件是否我们的主键,它将无法理解,只能返回一行。 所以我们需要告诉查询请使用Linq的FirstOrDefault()方法返回一行。

所以对于你的情况只是在你的查询之后的FirstOrDefault()所以它只会产生一行而不是一组行。

In Linq

whenever we write select with where condition whether its our primary key it will not understand that can have only one row to return. so we need to tell the query please return only one row with FirstOrDefault() method of Linq.

so for your case just at FirstOrDefault() after your query so it will result only one row instead of set of rows.

更多推荐

本文发布于:2023-07-30 14:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338859.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   类型   Linq   System   type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!