嗨,我正在读一本名为"Beginning Visual C#2012 Programming"的书,这本书在第4章的lopping部分中给出了以下的例子.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChapterFourExcerciseFour
{
class Program
{
static void Main(string[] args)
{
double balance, interestRate, targetBalance;
int totalYears = 0;
//reading balance from the console and saving it into the balance
Console.WriteLine("Please Enter your balance");
balance = Convert.ToDouble(Console.ReadLine());
//reading interesrrate from the console and saving it into tht interesrrate
Console.WriteLine("What is your current interest rate");
interestRate = Convert.ToDouble(Console.ReadLine());
//reading targetbalance from the console and saving …Run Code Online (Sandbox Code Playgroud) 我正在尝试这个查询以获得所有城市的
var queryAllCustomers = from cust in loadedCustomData.Descendants("record")
select (string)cust.Element("City") ;
Run Code Online (Sandbox Code Playgroud)
所以它返回所有城市包括重复,但我只想得到不同的城市,即重复只有那些如何实现呢?
我一直试图这样做:
创建'N'任务以执行并持续运行此数量的一段时间,在这种情况下,一个任务完成,然后我应该开始一个新任务以保持相同数量的任务.
我不知道是否可以使用TaskScheduler处理这个问题,或者我必须创建一个自定义的TaskScheduler.
我认为可以使用的另一个选项是,当任务完成时使用TPL DataFlow Producer-Consumer,然后taskcheduler接受生产者生成的新任务.
问题是:当一个任务完成以保持相同数量的任务时,如何创建新任务?
我编写了一个函数,它将使用相对较新的async/await模式在线程上运行任何函数,并稍后通过可选的回调方法通知调用者.该功能非常简单,如下所示:
private async void DoOperationAsync<T>(Func<T> operation, Action<T> resultAction = null)
{
if (operation == null)
throw new ArgumentNullException("operation");
var result = await Task<T>.Factory.StartNew(operation);
// Notify someone that this finished
if (resultAction != null)
resultAction(result);
}
Run Code Online (Sandbox Code Playgroud)
这对于返回值的函数非常有效.它不适用于返回void的函数(或者我可能不够聪明,无法使其工作).我可以编写一个不承担返回类型的特殊情况.但是,我想知道一些C#泛型专家是否可以在这种情况下指出一种处理void的方法.是否有工作方式不涉及无效功能的特殊情况?
我使用 .net 4.5 和 MachineKey.Protect/MachineKey.Unprotect 来加密和解密值。我想知道当我们将代码部署到拥有多个服务器的生产环境时,MachineKey.Protect/MachineKey.Unprotect 是否可以在不同步计算机密钥的情况下正常工作?
这是解密的示例代码:
var bytes = Convert.FromBase64String(Token);
var decryValue = MachineKey.Unprotect(bytes, Purpose);
string plainText = Encoding.UTF8.GetString(decryValue);
Run Code Online (Sandbox Code Playgroud)
让我知道你的想法!
当我使用DataTablewith时Parallel.ForEach,我得到:
指数超出范围。必须为非负数且小于集合的大小。
请帮我解决它。
static void Main(string[] args)
{
DataTable dt = CreateTable();
dt.Columns.Add("C");
//Parallel.ForEach(Partitioner.Create(0, dt.Rows.Count), range =>
//{
// for (int j = range.Item1; j < range.Item2; j++)
// {
// dt.Rows[j]["C"] = dt.Rows[j]["A"] + "-" + dt.Rows[j]["B"];
// }
//});
Parallel.ForEach(dt.AsEnumerable(), row =>
{
row["C"] = row["A"] + "-" + row["B"];
});
}
private static DataTable CreateTable()
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("A");
dataTable.Columns.Add("B");
for (int i = 0; i < 100000; i++)
{ …Run Code Online (Sandbox Code Playgroud) 我们对此很陌生,但我们正在尝试使用新的异步datareader
我们一直在关注http://blogs.msdn.com/b/adonet/archive/2012/07/15/using-sqldatareader-s-new-async-methods-in-net-4-5-beta上的示例-Part -2- examples.aspx
但遇到问题.我们要实现的目标是: - 存储过程返回2个表,使用datareader,我们尝试异步填充2个模型,即2个模型将同时创建,而不必等待第一个模型在第二个开始之前建立(如果这有意义?)
以下是我们目前的情况: -
public async Task<UsertResultsModel> GetResults(string user_ID)
{
UsertResultsModel rm = new UsertResultsModel();
List<TableColumn> ltc = new List<TableColumn>();
List<List<TableColumn>> lltc = new List<List<TableColumn>>();
var col = 0;
try
{
using (SqlConnection con = new SqlConnection(Config.DatabaseStringCDA))
{
await con.OpenAsync();
SqlCommand com = new SqlCommand(@"USP_GET_USER_RESULTS", con);
com.CommandType = CommandType.StoredProcedure;
using (SqlDataReader rdr = await com.ExecuteReaderAsync())
{
col = rdr.FieldCount;
while (await rdr.ReadAsync())
{
for (int i = 0; i < rdr.FieldCount; i++) …Run Code Online (Sandbox Code Playgroud) 我正试图从维基百科页面中提取内部链接.这是我正在使用的查询
/w/api.php?action=query&prop=links&format=xml&plnamespace=0&pllimit=max&titles=pageTitle
Run Code Online (Sandbox Code Playgroud)
但是,结果并不反映维基页面上的内容.以这里的随机文章为例.此页面上只有十几个链接.但是,当我进行查询时,
/w/api.php?action=query&prop=links&format=xml&plnamespace=0&pllimit=max&titles=Von_Mises%E2%80%93Fisher_distribution
Run Code Online (Sandbox Code Playgroud)
我找回了187个链接.我想API可能有一个数据库,其中包含已添加到页面的所有链接,包括所有修订版本.是这样的吗?如何从最后一次修订中获取链接?
我想知道我是否可以用foreach某种方式用LINQ查询替换它(如果可能的话):
在线围栏:https://ideone.com/PQEytf
using System;
using System.Collections.Generic;
using System.Linq;
public class Test
{
public static void Main()
{
// dummy inputs for test sake
var keys = new[] { "key1", "key2" };
var services = new Dictionary<string, Service>
{
{"key1", new Service {Components = new Dictionary<string, string> {{"comp1", "value1"}}}},
{"key2", new Service {Components = new Dictionary<string, string> {{"comp2", "value2"}}}}
};
var serviceComponents = GetServiceComponents(keys, services);
// do something with it
}
public static IEnumerable<ServiceComponent> GetServiceComponents(string[] keys, …Run Code Online (Sandbox Code Playgroud) 最近我用F#开始冒险.我正在尝试创建我将在C#项目中使用的F#库.
现在我面临的问题是我有两种类型的定义(如我所愿)可以自己使用(我正在尝试为c#用法创建流畅的API).
我想如何在c#中使用它(简化示例).
Shopping shopping = new Shopping();
Stuff[] stuff = shopping.GoTo("Wallmart").Buy(new [] { "Candies", "Ice cream", "Milk" }).GoTo("Drug store").Buy(new [] { "Anvil" }).GetStuff();
Run Code Online (Sandbox Code Playgroud)
现在我有两种类型(在separted文件中):
type ShopResult(someContext: ShoppingContext) =
//some logic
member this.GoTo shopName = new ToDoResult(someContext)
type ToDoResult(someContext: ShoppingContext) =
//some logic
member this.Buy what = new ShopResult(someContext)
Run Code Online (Sandbox Code Playgroud)
现在文件顺序导致编译错误,我想知道我的案例是否有任何解决方案?还是让我放弃流畅的api想法?