本文整理汇总了C#中IQueryable.SingleOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# IQueryable.SingleOrDefault方法的具体用法?C# IQueryable.SingleOrDefault怎么用?C# IQueryable.SingleOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IQueryable
的用法示例。
在下文中一共展示了IQueryable.SingleOrDefault方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BaseSingleResult
/// <summary>
/// Initializes a new instance of the <see cref="BaseSingleResult" /> class.
/// </summary>
/// <param name="query">The query that returns an object.</param>
/// <param name="edmType">The EDM type reference of the object.</param>
/// <param name="context">The context where the action is executed.</param>
protected BaseSingleResult(IQueryable query, IEdmTypeReference edmType, ApiContext context)
: base(edmType, context)
{
Ensure.NotNull(query, "query");
this.Result = query.SingleOrDefault();
}
示例2: EntityResult
/// <summary>
/// Initializes a new instance of the <see cref="EntityResult" /> class.
/// </summary>
/// <param name="query">The query that returns an entity.</param>
/// <param name="edmType">The EDM type reference of the entity.</param>
/// <param name="context">The context where the action is executed.</param>
public EntityResult(IQueryable query, IEdmTypeReference edmType, DomainContext context)
: base(edmType)
{
Ensure.NotNull(query, "query");
Ensure.NotNull(context, "context");
this.Context = context;
this.Result = query.SingleOrDefault();
}
示例3: GetPartInventory
public static Inventory GetPartInventory(IQueryable<Inventory> inventories, long partInfoId)
{
return inventories.SingleOrDefault(i => i.PartInfoId == partInfoId);
}
示例4: _GetUser
private MembershipUser _GetUser(SiteDB db, IQueryable<User> qUsers, bool userIsOnline)
{
User user = qUsers.SingleOrDefault();
UpdateLastLogin(db, user, userIsOnline);
return BuildMemberObject(user);
}
示例5: CreateQueryResponse
private HttpResponseMessage CreateQueryResponse(
IQueryable query, IEdmType edmType, bool isIfNoneMatch, ETag etag)
{
IEdmTypeReference typeReference = GetTypeReference(edmType);
BaseSingleResult singleResult = null;
HttpResponseMessage response = null;
if (typeReference.IsPrimitive())
{
if (this.shouldReturnCount || this.shouldWriteRawValue)
{
var rawResult = new RawResult(query, typeReference, this.Api.Context);
singleResult = rawResult;
response = this.Request.CreateResponse(HttpStatusCode.OK, rawResult);
}
else
{
var primitiveResult = new PrimitiveResult(query, typeReference, this.Api.Context);
singleResult = primitiveResult;
response = this.Request.CreateResponse(HttpStatusCode.OK, primitiveResult);
}
}
if (typeReference.IsComplex())
{
var complexResult = new ComplexResult(query, typeReference, this.Api.Context);
singleResult = complexResult;
response = this.Request.CreateResponse(HttpStatusCode.OK, complexResult);
}
if (typeReference.IsEnum())
{
if (this.shouldWriteRawValue)
{
var rawResult = new RawResult(query, typeReference, this.Api.Context);
singleResult = rawResult;
response = this.Request.CreateResponse(HttpStatusCode.OK, rawResult);
}
else
{
var enumResult = new EnumResult(query, typeReference, this.Api.Context);
singleResult = enumResult;
response = this.Request.CreateResponse(HttpStatusCode.OK, enumResult);
}
}
if (singleResult != null)
{
if (singleResult.Result == null)
{
// Per specification, If the property is single-valued and has the null value,
// the service responds with 204 No Content.
return this.Request.CreateResponse(HttpStatusCode.NoContent);
}
return response;
}
if (typeReference.IsCollection())
{
var elementType = typeReference.AsCollection().ElementType();
if (elementType.IsPrimitive() || elementType.IsEnum())
{
return this.Request.CreateResponse(
HttpStatusCode.OK, new NonResourceCollectionResult(query, typeReference, this.Api.Context));
}
return this.Request.CreateResponse(
HttpStatusCode.OK, new ResourceSetResult(query, typeReference, this.Api.Context));
}
var entityResult = query.SingleOrDefault();
if (entityResult == null)
{
return this.Request.CreateResponse(HttpStatusCode.NoContent);
}
// Check the ETag here
if (etag != null)
{
// request with If-Match header, if match, then should return whole content
// request with If-Match header, if not match, then should return 412
// request with If-None-Match header, if match, then should return 304
// request with If-None-Match header, if not match, then should return whole content
etag.EntityType = query.ElementType;
query = etag.ApplyTo(query);
entityResult = query.SingleOrDefault();
if (entityResult == null && !isIfNoneMatch)
{
return this.Request.CreateResponse(HttpStatusCode.PreconditionFailed);
}
else if (entityResult == null)
{
return this.Request.CreateResponse(HttpStatusCode.NotModified);
}
}
// Using reflection to create response for single entity so passed in parameter is not object type,
// but will be type of real entity type, then EtagMessageHandler can be used to set ETAG header
// when response is single entity.
//.........这里部分代码省略.........
示例6: ValidateEtag
/// <summary>
/// Validate the e-tag via applies the current DataModificationItem's OriginalValues to the
/// specified query and returns result.
/// </summary>
/// <param name="query">The IQueryable to apply the property values to.</param>
/// <returns>
/// The object is e-tag checked passed.
/// </returns>
public object ValidateEtag(IQueryable query)
{
Ensure.NotNull(query, "query");
Type type = query.ElementType;
ParameterExpression param = Expression.Parameter(type);
Expression where = null;
if (this.OriginalValues != null)
{
foreach (KeyValuePair<string, object> item in this.OriginalValues)
{
if (!item.Key.StartsWith("@", StringComparison.Ordinal))
{
where = ApplyPredicate(param, where, item);
}
}
if (this.OriginalValues.ContainsKey(IfNoneMatchKey))
{
where = Expression.Not(where);
}
}
LambdaExpression whereLambda = Expression.Lambda(where, param);
var queryable = ExpressionHelpers.Where(query, whereLambda, type);
var matchedResource = queryable.SingleOrDefault();
if (matchedResource == null)
{
// If ETAG does not match, should return 412 Precondition Failed
var message = string.Format(
CultureInfo.InvariantCulture,
Resources.PreconditionCheckFailed,
new object[] { this.DataModificationItemAction, query.SingleOrDefault() });
throw new PreconditionFailedException(message);
}
return matchedResource;
}
示例7: GetCode
public string GetCode(string val, IQueryable<Warehouse> list)
{
if (UseVIdAsValue || (this.Type == WarehouseType.Part))
{
long id; long.TryParse(val, out id);
var wh = list.SingleOrDefault(w => w.WarehouseId == id);
return (wh == null) ? "" : wh.Code;
}
else
{
return val;
}
}
示例8: GetRegistration
/// <summary>
/// Get registration from OriginalWriteEventId.
/// </summary>
public static Registration GetRegistration(IQueryable<Registration> dataSet, int originalWriteEventId)
{
return dataSet
.SingleOrDefault(r => r.OriginalWriteEventId == originalWriteEventId);
}