我有以下代码,我从一个源填充用户,为例子如下所示.我想要做的是消耗多个消费者的BlockingCollection.
低于正确的方式吗?还有什么是最好的线程数?好吧这取决于硬件,内存等.或者我怎么能以更好的方式做到这一点?
还将在下面的实现确保我将处理集合中的所有内容,直到它为空?
class Program
{
public static readonly BlockingCollection<User> users = new BlockingCollection<User>();
static void Main(string[] args)
{
for (int i = 0; i < 100000; i++)
{
var u = new User {Id = i, Name = "user " + i};
users.Add(u);
}
Run();
}
static void Run()
{
for (int i = 0; i < 100; i++)
{
Task.Factory.StartNew(Process, TaskCreationOptions.LongRunning);
}
}
static void Process()
{
foreach (var user in users.GetConsumingEnumerable())
{
Console.WriteLine(user.Id);
}
}
}
public …Run Code Online (Sandbox Code Playgroud) public class GeoHelper
{
const string GeoIpUrl = "http://freegeoip.net/json/{0}";
private readonly string _ipAddress = string.Empty;
public GeoHelper(string ip)
{
_ipAddress = ip;
}
public async Task<string> GetGeoAsync()
{
string uri = string.Format(GeoIpUrl, _ipAddress);
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(uri);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
return content;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我按如下方式调用它:
[ChildActionOnly]
public ActionResult UserGeo()
{
var ip = RequestHelper.GetClientIpAddress(Request);
var geoHelper = new GeoHelper(ip);
var response = geoHelper.GetGeoAsync();
var result = response.Result;
var resultobj = JsonConvert.DeserializeObject<GeoInfo>(result); …Run Code Online (Sandbox Code Playgroud) 我们有一项调查.我有一个模型如下:
Survey
City
Satisfaction
Id
Run Code Online (Sandbox Code Playgroud)
满意度是一个枚举如下:
Satisfaction:
VerySatisfied,
Satisfied
Undecided,
NotSatisfied
Run Code Online (Sandbox Code Playgroud)
我对按城市分组感兴趣,然后我想通过非常满意和满意的分组.
var result= _db.Surveys.AsNoTracking()
.GroupBy(x => x.City)
.Select(group => new {
City = group.Key.Code,
CityName = group.Key.Name,
Count = group.Count()
})
.OrderByDescending(x => x.Count);
Run Code Online (Sandbox Code Playgroud)
我可以按城市分组,但我需要找到城市内的满意度.
即:纽约:%90%.
我如何分组两次以获得上述结果?
我使用通用列表将不同实体的数据填充到一组列表中,如下所示:
List<Foo> foos ..
List<Bar> bars ..
Run Code Online (Sandbox Code Playgroud)
我需要将这些列表写入文件,我有一个util方法来使用反射来获取属性的值等.
我想要做的是:使用单一方法将这些文件写入文件,例如:
void writeToFile(a generic list)
{
//Which i will write to file here.
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我希望能够打电话:
writeToFile(bars);
writeToFile(foos);
Run Code Online (Sandbox Code Playgroud) 在asp.net应用程序上实现为用户创建和赋予ID的方法的最佳方法是什么?
我正在考虑使用DateTime刻度和线程ID
我想确保没有碰撞,用户ID是唯一的.
ID可以是字符串或长.
我应该在用户收集的一些信息上使用MD5吗?会是什么?
我看到md5的碰撞率非常低.
在什么情况下你会推荐使用UTF-8?是否有替代方案可以达到相同的目的?
i18n 使用 UTF-8?
我有如下属性集:
public string Foo1 {set;get;}
public string Foo2 {set;get;}
public string Foo3 {set;get;}
public string Foo4 {set;get;}
public string Foo5 {set;get;}
public string Foo6 {set;get;}
public string Foo7 {set;get;}
public string Foo8 {set;get;}
public string Foo9 {set;get;}
......
public string Foo50 {set;get;}
Run Code Online (Sandbox Code Playgroud)
然后我迭代一个集合,如下所示:
foreach(var element in sortedCollection.Keys){
if(element != null)
// in this block I would like to assign the element to the properties above
// ex:
foo?? = sortedCollection[element];
// ?? need to be replaced by index.
}
Run Code Online (Sandbox Code Playgroud)
是否有捷径可寻?
我的同事正在捍卫为应用程序打开单个数据库连接比使用池打开和关闭它更好,更快.
他有一个ApplicationStart方法,Application('db')可以在应用程序中实现并保持此连接.这个应用程序主要包含只读数据.
我怎么能说服他?
我有一个关联数组,在迭代这个数组时,使用foreach循环.Flex正在失去订单.这太烦人了.
为什么会这样?
我怎么能避免这个?
列表已排序.
我有一个List,我想对它进行二分搜索.T有StartIndex,EndIndex等成员.
我可以使用StartIndex对列表进行二进制搜索,即:我为此实现了IComparable.
我需要稍微扭转一下如下:我想找到一个可能是OffBy一个小值的StartIndex.
例如:T.StartIndex = 100
如果输入为101且OffBy为1,则BinarySearch应返回此对象.
我怎样才能做到这一点?
顺便说一句,我想问一下如何用List的默认二元搜索方法.这就是我感兴趣的,对自定义二进制搜索实现不感兴趣.
c# ×7
.net ×4
algorithm ×1
apache-flex ×1
asp.net ×1
async-await ×1
collections ×1
database ×1
generics ×1
hash ×1
linq ×1
list ×1
localization ×1
methods ×1
reflection ×1
search ×1
utf-8 ×1