我有一个由多种方法调用的类。这个类是:
public class Policy : EntityObject
{
public Guid PolicyId { get; set; }
public Guid CustomerId { get; set; }
public string PolicyNumber { get; set; }
[NotMapped]
public string TrimmedPolicyNumber { get; set; }
public DateTime? PolicyEffectiveDate { get; set; }
public DateTime? PolicyExpirationDate { get; set; }
[NotMapped]
public string PolicyType { get; set; }
public string InsuranceCompany { get; set; }
public string WritingCompany { get; set; }
[NotMapped]
public string BillMethod_PaymentPlan { get; set; } …Run Code Online (Sandbox Code Playgroud) 我有这样的方法:
public List<List<string>> GroupedNodes(string URL, params string[] XPathes)
{
//Load HTML Source
HtmlWeb loader = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = loader.Load(URL);
//some other codes ...
//Return result as a List of list
return grouped;
}
Run Code Online (Sandbox Code Playgroud)
我使用HtmlAgilityPack从网址获取html源代码.
但是当我使用这种方法时,它会导致冻结程序.
我知道当我在UI中调用此方法时我可以使用多线程但是我想以异步和响应的方式编写我的方法,当我们使用它时,它可以在不冻结的情况下工作.
我的意思是如果有人使用我的方法并且他/她不了解多线程,我希望他/她的程序不要冻结,换句话说,我不想用线程或任务调用我的方法!
c# multithreading asynchronous async-await html-agility-pack
我正在疯狂尝试在我的类库中使用Json序列化器/反序列化器并在SQL Server中导入我的程序集。
我正在使用响应Json字符串的WebAPI,并且我想创建调用该API并使用api结果的CLR Sql Procedure。
我尝试了两种方法来反序列化Json字符串:
1)System.Web.Script.Serialization
2)System.Runtime.Serialization.Json
第一个让我看到这个错误:
程序集'system.web.extensions,版本= 4.0.0.0,区域性=中性,publickeytoken = 31bf3856ad364e35。” 在SQL目录中找不到。(Microsoft SQL Server,错误:6503)
第二个:
程序集'system.runtime.serialization,版本= 4.0.0.0,文化=中性,publickeytoken = b77a5c561934e089。' 在SQL目录中找不到。(Microsoft SQL Server,错误:6503)
有什么方法可以解析我的类库中的json吗?(除了在类库中为我自己创建一个Json Serializer / Deserializer之外!)
Visual Studio 2015社区,Microsoft Sql Server 2016开发人员
先感谢您。
当它存储在List中时,我想让我的Class Sortable(By Age).
我读到这个:IComparable和IComparer,我制作了我的类Sortable.
public class Student : IComparable<Student>
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int CompareTo(Student other)
{
if (this.Age > other.Age)
{
return 1;
}
else if (this.Age < other.Age)
{
return -1;
}
else
{
return 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
列出学生=新列表();
//并填补学生
students.Sort();
现在,我想让我的班级有意义,我的意思是当我打电话时.Distinct()它删除重复的学生按ID.
我读IEquatable VS IEqualityComparer 和Sort一样(没有任何争论者)我希望调用.Distinct()而不传递争论者.
public class Student : IEquatable<Student>
{
public int ID …Run Code Online (Sandbox Code Playgroud) 我编写了一个程序,列表构建器方法返回包含大量字符串(1milion项)的字符串的IEnumerable,并将其存储在字符串List中,然后将所有项附加到Parallel.Foreach中的StringBuilder实例中.然后我打印了stringBuilderInstance.Length.
问题是它少于1000000.经过一些谷歌搜索,我意识到列表集合不是线程安全的,这导致了这个问题.因此2解决方案贯穿我的脑海:
1)使用锁
2)使用ConcurrentBag
当我使用锁定时,它是可以的,长度是1百万,但是:
当我使用字符串的ConcurrentBag时,长度比我预期的要少!
这个问题的根本原因是什么?
List-Creator方法:
public static List<string> CreateList()
{
List<string> result = new List<string>();
for (int i = 0; i < 1000000; i++)
{
result.Add(1.ToString());
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
使用ConcurrentBag:
public static void DoWithParallel_ThreadSafe()
{
ConcurrentBag<string> listOfString = new ConcurrentBag<string>(CreateList());
StringBuilder a = new StringBuilder();
Action<string> appender = (number) =>
{
a.Append(number);
};
Parallel.ForEach(listOfString, appender);
Console.WriteLine($"The string builder lenght : {a.Length}");
}
Run Code Online (Sandbox Code Playgroud)
使用锁:
public static void DoWithParallel_UnsafeThread_Lock()
{ …Run Code Online (Sandbox Code Playgroud) 我已经构建了一个 Web Assembly blazor 项目。
在其 blazor 组件之一中,有一个用于提交新数据的表单。
这种形式,通过 Web api 将数据 POST 到服务器以保存在数据库中。
<EditForm Model="@person">
<div class="col-12 row">
<label class="col-2 font-weight-bold">Id : </label>
<InputNumber class="form-control col-3" @bind-Value="person.Id" placeholder="id" />
</div>
<br />
<div class="col-12 row">
<label class="col-2 font-weight-bold">Name : </label>
<InputText class="form-control col-3" @bind-Value="person.Name" placeholder="name" />
</div>
<input type="submit" class="form-control btn btn-primary text-center" @onclick="@AddPerson" value="Add" />
</EditForm>
Run Code Online (Sandbox Code Playgroud)
背后的 C# 代码@AddPerson是这样的:
@code
{
public List<Person> people;
public Person person = new Person();
private async void AddPerson()
{
HttpResponseMessage …Run Code Online (Sandbox Code Playgroud) c# ×6
async-await ×2
asynchronous ×2
blazor ×1
distinct ×1
iequatable ×1
json ×1
linq ×1
sql ×1
sql-server ×1
sqlclr ×1