在C#.net ConcurrentDictionary(C#参考源)的参考源代码中,我不明白为什么在以下代码片段中需要进行易失性读取:
public bool TryGetValue(TKey key, out TValue value)
{
if (key == null) throw new ArgumentNullException("key");
int bucketNo, lockNoUnused;
// We must capture the m_buckets field in a local variable.
It is set to a new table on each table resize.
Tables tables = m_tables;
IEqualityComparer<TKey> comparer = tables.m_comparer;
GetBucketAndLockNo(comparer.GetHashCode(key),
out bucketNo,
out lockNoUnused,
tables.m_buckets.Length,
tables.m_locks.Length);
// We can get away w/out a lock here.
// The Volatile.Read ensures that the load of the fields of 'n' …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using MongoDBTest;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace protocol.server.API.Clients
{
public class ClientService : ServiceStack.Service
{
class CylinderSerializer : SerializerBase<Cylinder>
{
public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, Cylinder value)
{
var wr = context.Writer;
wr.WriteStartDocument();
wr.WriteName("_id");
wr.WriteObjectId(ObjectId.GenerateNewId());
wr.WriteName("description");
wr.WriteString(value.description.type);
context.Writer.WriteEndDocument();
}
public override Cylinder Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
{
context.Reader.ReadStartDocument();
Cylinder a = new Cylinder();
a.Id = context.Reader.ReadObjectId();
while (context.Reader.State != BsonReaderState.Type && context.Reader.ReadBsonType() != …Run Code Online (Sandbox Code Playgroud) 我正在尝试使这项工作:
var customerSearchResult = customers.GroupBy(
x => new {
x.CustomerID,
x.email,
x.CreatedOn,
x.FirstName,
x.LastName,
x.Profile == null ? -1 : x.Profile.Value
})
.Select(csr => new CustomerSearchResult
{
CustomerID = csr.Key.CustomerID,
Email = csr.Key.email,
CreatedOn = csr.Key.CreatedOn
});
Run Code Online (Sandbox Code Playgroud)
我得到了一个
错误CS0746无效的匿名类型成员声明符.必须使用成员分配,简单名称或成员访问声明匿名类型成员.
因为这行x.Profile == null ? -1 : x.Profile.Value
Profile可以为null.
知道怎么做吗?
我对 Mongodb 和 C# 驱动程序完全陌生。
开发是在 Ubuntu 14.04 上使用 Monodevelop 完成的,Mongodb 的版本是 3.2.10 :
目前我的代码有一个 POCO 如下:
public class User
{
public String Name { get; set;}
public DateTime LastModifiedAt { get; set;}
public DateTime LastSyncedAt { get; set;}
public User ()
{
}
}
Run Code Online (Sandbox Code Playgroud)
已经能够创建集合并添加用户。
如何查找 LastModifiedAt 时间戳大于 LastSyncedAt 时间戳的用户?进行了一些搜索,但未能找到答案。
任何建议都会有很大的帮助
谢谢
我看到一些同事的代码,他选择不等待数据库调用而只返回任务。例如
public Task<UpdateResult> AddActivityAsync(ClaimsPrincipal principal, Activity activity)
{
return _userManager.SaveToDatabaseAsync(principal, activity);
}
Run Code Online (Sandbox Code Playgroud)
与_userManager.SaveToDatabaseAsync异步一样,我会以这种方式实现
public async Task<UpdateResult> AddActivityAsync(ClaimsPrincipal principal,
Activity activity)
{
return await _userManager.SaveToDatabaseAsync(principal, activity);
}
Run Code Online (Sandbox Code Playgroud)
这个方法的调用方法总是等待它:
await _profile.AddActivityAsync(..., ...)
Run Code Online (Sandbox Code Playgroud)
不使内部方法异步并只返回任务,让调用者等待它有什么好处吗?我以为我们必须一直写 Async ......
所以我有一个JSON字符串,我只想读取一个特定的值.我如何Read me please!从下面的字符串中选择" "?
var readString = /*Read me please!*/
Run Code Online (Sandbox Code Playgroud)
JSON字符串:
"{\"aString\":\"Read me please!\"}"
Run Code Online (Sandbox Code Playgroud)
为了更好地理解,我如何在这里做同样的事情?(只是" Read me please!"):
"{\"Result\":
{
\"aString\":\"Read me please!\",
\"anotherString\":\"Dont read me!\"
}
}"
Run Code Online (Sandbox Code Playgroud)
如果两种方案都有不同的解决方案,我想知道两者.
PS:我不希望将值保存到object/class左右.只是暂时在里面var readString.
我有以下Lambda表达式,它返回一个结果属性数组:
ViewBag.Items = db.Items.Select(m => new { m.Id, m.Column1, m.Column2 }).ToArray();
Run Code Online (Sandbox Code Playgroud)
这按预期工作,但我需要结果是一个键=>值对,其中Id是关键,Column1和Column2是值.我找到了关于如何创建Dictionary的示例,但没有找到仅隔离结果中的特定列的示例.
ViewBag.Items 使用以下命令在View中为jQuery本地化:
var _items = @Html.Raw(Json.Encode(ViewBag.Items));
Run Code Online (Sandbox Code Playgroud)
如何更改上面的Lambda以生成key => value对数组?
我有一个对象列表(List1)和字符串List2列表(- 对象名称列表)
如果List2中不存在object.Name,我需要从List1获取所有对象
怎么写这个LINQC#.?
我正在尝试为函数编写单元测试,但我需要在测试之前启动一些变量.
我需要测试的函数有一个参数 Task<List<string>> list
我想发起这个变量.
这是我到目前为止所尝试的:
Task<List<string>> list = new List<string>();
Run Code Online (Sandbox Code Playgroud) 在C#中我可以抛出溢出异常:
throw new System.OverflowException("Cannot push onto a full stack.");
Run Code Online (Sandbox Code Playgroud)
如何抛出下溢异常?
throw new System.UnderflowException("Cannot pop from an empty stack.");
Run Code Online (Sandbox Code Playgroud)
它看起来不像UnderflowException是一种方法System.
c# ×10
.net ×5
linq ×3
mongodb ×2
async-await ×1
asynchronous ×1
exception ×1
group-by ×1
json ×1
lambda ×1
list ×1
memory-model ×1
task ×1
volatile ×1