无法转换为system.guid

时间:2013-03-18 20:12:04

标签: linq entity-framework c#-4.0

我们正在使用实体框架


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'

2 个答案:

答案 0 :(得分:1)

您的问题在于此比较:

m.OperatorId = (from o in Entities.t_CellSite
    where o.CellSiteId == Guid.Parse("53B7B160-20C4-4B60-948A-06570E6E3CBA")
    select o.OperatorId)

您正在将System.GuidSystem.Linq.IQueryable<System.Guid>进行比较。如果您只期望一个结果,那么您可以这样做:

m.OperatorId = (from o in Entities.t_CellSite
    where o.CellSiteId == Guid.Parse("53B7B160-20C4-4B60-948A-06570E6E3CBA")
    select o.OperatorId).First()

答案 1 :(得分:1)

我认为这应该解决它:

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进行比较,并且您将它与LINQ查询进行比较,后者返回IQuerable - 在LINQ查询上调用Single()执行它并返回一个值来进行比较。