如何从返回“的Guid'';的Guid>可空&LT”?Guid、LT、GT

2023-09-05 23:06:16 作者:∴专业三秒男∵

我想返回一个的Guid 值以下。但是数据库(和我的dbml 的)有一个列的可空的Guid ,它是产生于例外。选择部分说,它不能从的IQueryable&LT转换;的System.Guid> 的System.Guid

我猜我需要让我的返回值的的具体的的第一????真的吗? 如果是的话我怎么做,与的GUID?

 公共静态的Guid GetCurrentWorkerByType(INT enrollmentID,INT staffTypeID)
{
    使用(VAR上下文= CmoDataContext.Create())
    {
        IQueryable的< tblWorkerHistory> tWorkHist = context.GetTable&其中; tblWorkerHistory>();

        返程(tWorkHist.Where(workHist =>
            (workHist.EnrollmentID == enrollmentID)及&安培;
            (workHist.tblStaff.StaffTypeID == staffTypeID)及&安培;
            (workHist.EndDate == NULL || workHist.EndDate> DateTime.Now))
            。选择(workHist => workHist.Worker));
        }
    }
}
 

解决方案

  //获取的GUID?本身,对于为了举例从IQueryable的&所述;的Guid&GT?;
//(可能是`null`从DB值,或因为可查询的是空)
GUID? maybeGuid = queryable.FirstOrDefault();
//需要有* A * GUID或默认情况下它的东西
//因为一个GUID是一个价值型和需求* A *值。
GUID theGuid = maybeGuid? Guid.Empty;
 

另请参见可空< T> .HasValue /值 - 较长,但等效,方法是:

 的Guid theGuid = maybeGuid.HasValue? maybeGuid.Value:Guid.Empty;
 
PSD教程 手把手教你制作金属复古质感字效

观察到 hasVa​​lue的可适合一般如果语句来更改逻辑和还指出,如果maybeGuid是没有价值,将抛出一个异常 - 是 - 这就是为什么保护是必需的

快乐编码。

迂腐的细节:等效方法是不是线程安全。也就是说,假设 maybeGuid 是共享的,它可以被分配 hasVa​​lue的和。有一些做题,涵盖了线程安全 ?? (聚结运营商) - 产生IL有效地使用一个临时变量,因此该数值可能陈旧但异常不能被抛出。

I am trying to return a Guid value below. However the database(and my dbml) has that column as a nullable Guid and it is generating an exception on the .Select portion saying it can't convert from an IQueryable<System.Guid?> to a System.Guid.

I am guessing I need to make my return value "concrete" first???? True? If so how do I do that with Guid's?

public static Guid GetCurrentWorkerByType(int enrollmentID, int staffTypeID)
{
    using (var context = CmoDataContext.Create())
    {
        IQueryable<tblWorkerHistory> tWorkHist = context.GetTable<tblWorkerHistory>();

        return (tWorkHist.Where(workHist => 
            (workHist.EnrollmentID == enrollmentID) &&
            (workHist.tblStaff.StaffTypeID == staffTypeID) &&
            (workHist.EndDate == null || workHist.EndDate > DateTime.Now))
            .Select(workHist => workHist.Worker));
        }
    }
}

解决方案

// Get the Guid? itself, for sake of example from IQueryable<Guid?>
// (could be `null` from DB value or because queryable was empty)
Guid? maybeGuid = queryable.FirstOrDefault();
// Need to have *a* GUID or default it to something
// because a Guid is a value-type and needs *a* value.
Guid theGuid = maybeGuid ?? Guid.Empty;

Also see Nullable<T>.HasValue/Value -- A longer, but equivalent, method would be:

Guid theGuid = maybeGuid.HasValue ? maybeGuid.Value : Guid.Empty;     

Observe that HasValue may be suitable in general if statements to change logic and also note that Value will throw an exception if maybeGuid is "has no value" -- is null -- which is why the guard is required.

Happy coding.

Pedantic detail: The equivalent method is not "thread safe". That is, assuming maybeGuid was shared, it could be assigned null between HasValue and Value. There are a number of SO questions that cover the "thread safety" of ?? (the coalesce operator) -- the generated IL effectively uses a temporary variable so the value may be stale but an exception can't be thrown.