gpt4 book ai didi

c# - 运行程序后无法将类型 'System.Linq.IQueryable' 转换为 'System.Guid'

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:12 26 4
gpt4 key购买 nike

我是 C# 和实体的新手 我想知道是否有人在这里帮助我。我选择了哪个返回我的 customerid,所以我想将它作为参数传递给我的构造函数,我的构造函数参数类型是 guid 但我的选择类型不同,我不知道如何将它转换为 guid。这是我的代码:

namespace FactorEntity
{
public partial class CustomerResearchForm : MetroFramework.Forms.MetroForm
{
FactorEntities contex;
MenuForm _customerform;

void CResearchGrid_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
var CustomerUpdateAndDelete =
new CustomerUpdateAndDelete();
contex = new FactorEntities();
var sendergrid=(DataGridView)sender;
var customercode =
var cellValue =
sendergrid.Rows[e.RowIndex].Cells[1].Value;
Convert.ToInt32(cellValue);
var customer = from _customer in contex.tblCustomers
where _customer.CustomerCode==customercode
select _customer.CustomerID;
var _CustomerId=(Guid)(customer);
var customerform =
new CustomerForm(_customerform,_CustomerId);
customerform.Show();
}
}
}

错误在这一行:Guid _CustomerId=(Guid)(customer);提前致谢。

最佳答案

linq 查询没问题,但是 Where 方法返回一个 IQueryableSystem 类型,它就像一个结果列表。您必须选择第一个结果或将查询更改为仅将返回类型更改为 1 个元素。

我会更改此表单中的 linq 查询以获取第一个元素:

var customers = from _customer in contex.tblCustomers where
_customer.CustomerCode==customercode select _customer.CustomerID;

var customer = customers.First();

或者如果您知道该查询将仅生成 1 个元素或将抛出错误,则使用此查询仅获取 1 个元素:

var customer = contex.tblCustomers.Single(x => x.CustomerCode == customercode);

关于c# - 运行程序后无法将类型 'System.Linq.IQueryable<System.Guid>' 转换为 'System.Guid',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43987632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com