linq - 无法转换为 system.guid

标签 linq entity-framework c#-4.0

我们正在使用 Entity Framework


private t_Market getMarketByCellsiteID(Guid cellsiteID)
{
    try
    {
        t_Market market = null;
        using (LiveLeaseEntities Entities = new LiveLeaseEntities())
        {
            market = (from m in Entities.t_Market
                      where m.OperatorId = (from o in Entities.t_CellSite
                                            where o.CellSiteId == Guid.Parse("53B7B160-20C4-4B60-948A-06570E6E3CBA")
                                            select o.OperatorId)
                      select m).Single();
            return market;
        }
    }

我得到无法将类型“System.Linq.IQueryable”隐式转换为“System.Guid”

最佳答案

我认为这应该可以解决:

private t_Market getMarketByCellsiteID(Guid cellsiteID)
{
try
{
    t_Market market = null;
    using (LiveLeaseEntities Entities = new LiveLeaseEntities())
    {
        market = (from m in Entities.t_Market
                  where m.OperatorId == (from o in Entities.t_CellSite
                                        where o.CellSiteId == Guid.Parse("53B7B160-20C4-4B60-948A-06570E6E3CBA")
                                        select o.OperatorId).Single()
                  select m).Single();
        return market;
    }
}

两件事:您需要一个 == 来与 m.OperatorId 进行比较,并且您正在将它与返回 IQuerable 的 LINQ 查询进行比较——调用LINQ 查询上的 Single() 执行它并返回一个值进行比较。

关于linq - 无法转换为 system.guid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15485996/

相关文章:

c# - 基于服务的数据库连接字符串

sql-server - 多对多链接表的 Entity Framework 命名约定

c# - 将工作簿保存到流而不是文件中

c# - 使用 LINQ 创建 XML

c# - 我是否正确使用了 LINQ .OfType() 运算符?

c# - 通用 linq 排序方法 - 解析表达式

c# - C# 3.0 中的命名/可选参数?

c# - 用于比较嵌套数组的 LINQ 表达式

.NET Entity Framework "remembering"数据库名称不存在

c#-4.0 - WIF 4.5 声明值 - 未将对象引用设置为对象的实例