小编Joh*_*ica的帖子

LINQ GroupBy 仅在键不为空时选择

这可能是一个简单的答案。

我正在制作这个 LINQ 表达式,其中按“名称”对列表进行分组,然后创建一个 SymbolField 集合,其中组键(名称)用作名称,值作为第二个参数连接在一起。

但问题更多的是,如何避免可能的空引用?

如果您查看图片,您会发现“可能”存在 为group.keynull 的情况。

我只想选择那些不为空的。我怎样才能根据我的代码做到这一点?

return result
    .GroupBy(r => r.Name)
    .Select(group =>
        new SymbolField(group.Key, string.Join(string.Empty, group.Select(g => g.Symbol))));
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

c# linq null c#-8.0 nullable-reference-types

2
推荐指数
1
解决办法
1095
查看次数

如何连接两个字节数组

我使用这种方法合并现有的 pdf 文档并向其中添加新的 pdf 文档(在内存中不是物理的)问题我有 2 个内存流,一个用于合并的 pdf,一个用于新文档,然后将其转换为字节数组,我想连接这些拖车阵列

public static byte[] merge(List<String> pdf)
{
    MemoryStream copystream;
    MemoryStream ms;
    using (ms = new MemoryStream())
    {
        Document document;
        using (document = new Document())
        {
            using (PdfWriter wri = PdfWriter.GetInstance(document, ms))
            {
                wri.CloseStream = false;
                document.Open();
                document.SetPageSize(iTextSharp.text.PageSize.A4); // for vertical layout

                document.Add(new Paragraph("Hello"));

                document.Close();

                copystream = new MemoryStream();
                Document doc = new Document();
                using (PdfCopy copy = new PdfCopy(doc, copystream))
                {
                    copy.CloseStream = false;
                    copy.Open();
                    doc.Open();

                    // copy.AddPage(PageSize.A4, 0);
                    for (int i = …
Run Code Online (Sandbox Code Playgroud)

c# pdf arrays memorystream

1
推荐指数
1
解决办法
8421
查看次数

给定另一个列表生成缺失元素列表的有效方法?

我有一个包含元素的列表[1,2,3,4,5]

如果我有另一个列表包含主列表的元素,例如[1,2][2,4,5],那么生成具有缺失数字的新列表的有效方法是什么,以便:

[1, 2]利用[1,2,3,4,5]会给[3, 4 ,5]

[2, 4, 5]利用[1,2,3,4,5]会给[1, 3]

我正在考虑使用嵌套的 for 循环来检查,但我想知道是否有更有效的方法或内置函数可以在 C# 中使用。

c# list

1
推荐指数
1
解决办法
671
查看次数

交易搜索始终为空

我尝试搜索所有交易,我总是空集合.在我的paypal帐户中我收到了很多交易.

我尝试了任何其他请求,并且我从所有请求中都是空的

BraintreeGateway gw = new BraintreeGateway("access_token$...");
var request = new TransactionSearchRequest().Status.IncludedIn(TransactionStatus.ALL);
var collection = gw.Transaction.Search(request);

foreach (Braintree.Transaction transaction in collection)
{
    Console.WriteLine(transaction.Id);
}
Run Code Online (Sandbox Code Playgroud)

c# paypal braintree

1
推荐指数
1
解决办法
54
查看次数

当存储在天蓝色斑点存储中时,如何获取图像尺寸?

我在Blob中有图像,我想读取图像尺寸(宽度和高度)

这是我从blob中读取的功能:

 private static CloudBlockBlob ReadBlockBlob(string input, IConfigurationRoot config)
    {
        CloudStorageAccount storageAccount = new CloudStorageAccount(
            new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(config["storageAccountName"], config["storageKeyValue"]), true);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(config["blobContainer"]);
        return container.GetBlockBlobReference(input);
    }
Run Code Online (Sandbox Code Playgroud)

我在CloudBlobContainer和CloudBlockBlob中都看不到任何有用的元数据。

知道如何从Blob获取图像尺寸吗?

谢谢

c# azure azure-storage-blobs

1
推荐指数
1
解决办法
416
查看次数

将枚举映射到 json 属性

我有一个名为 InstrumentConfigValues 的类,其属性具有实现接口的类型。现在我有一个名为 InstrumentConfig 的枚举,它具有一组值。这些值就像 json 文件中的键。我想映射类似[JsonProperty(InstrumentConfig.LowDiskpace.ToString()].

出于某种原因,它不允许这样做并抱怨说:

属性参数必须是常量表达式

我特别提到了许多帖子JsonStringEnumConverter。但是如何使用枚举键映射每个属性。我也看到了这篇文章JsonSerializationSettings但无法与我的问题相关联。请帮忙/

public class InstrumentConfigValues : IInstrumentConfig
{      
    public double SpaceNeededForSingleRun
    {
        get; set;
    }

    public int NumberOfInputSlots
    {
        get; set;
    }

    public int SupportedChannelCount
    {
        get; set;
    }
}

//I want this inheritance as some other class wants to access the values.
public abstract class InstrumentConfigReadWrite : InstrumentConfigValues
{
    protected ReturnCodes PopulateValuesFromJObject(JObject jObject, string path)
    {
        try
        {
            if (JsonConvert.DeserializeObject<InstrumentConfigValues>(jObject.ToString()) == null)
            {
                return ReturnCodes.ErrorReadingFile; …
Run Code Online (Sandbox Code Playgroud)

c# json.net

1
推荐指数
1
解决办法
474
查看次数

具有方法的接口包含 ValueTuple

我有界面:

public Interface Inter1
{
 ....
 Task<(ITestBulk testBulk, bool resultBulk)> GetBulk(int par1, int par2, string par3);
}
Run Code Online (Sandbox Code Playgroud)

然后得到了一些方法:

 class MyClass{

   internal bool m_resultBulk;

 internal async void NewBulk()
 {
   ....
   (ITestBulk, m_resultBulk) = await service.GetBulk(1, 2, "ggg");
 } 
}
Run Code Online (Sandbox Code Playgroud)

m_resultBulk找不到。我做错了什么?

c# tuples interface task async-await

1
推荐指数
1
解决办法
571
查看次数

Froma list of objects, 如何在另一个参数值中获取特定对象参数值的列表

不确定我做错了什么。我一直在互联网上寻找如何使用 lambda 有效地获取基于另一个属性的属性值列表。

假设我有一个包含教师姓名和房间 ID 的教室列表:

public class ClassRoom
{
    public string Teacher;
    public int RoomId;

}

List<ClassRoom> classRooms = new List<ClassRoom>();

classRooms.Add(new ClassRoom() { RoomId = 2000, Teacher = "Mr. Taylor" });
classRooms.Add(new ClassRoom() { RoomId = 2010, Teacher = "Mrs. Lee" });
classRooms.Add(new ClassRoom() { RoomId = 3050, Teacher = "Mrs. McNamara" });
classRooms.Add(new ClassRoom() { RoomId = 4090, Teacher = "Mr. Taylor" });
Run Code Online (Sandbox Code Playgroud)

然后,我需要一个特定老师的房间 ID 列表。

List<int> RoomIds = new List<int>();

    foreach(ClassRoom classRoom in classRooms)
    {
        if(classRoom.Teacher …
Run Code Online (Sandbox Code Playgroud)

c# linq lambda list

1
推荐指数
1
解决办法
87
查看次数

正确使用调用

我有一个关于 invoke 用法的一般问题。我的大多数 C# winforms 项目都有一个后台工作人员和一个 UI。很快我意识到我需要来自后台工作人员 UI 的信息,或者我需要更改我的后台工作人员的 UI。例子:

//example invoke usage to get information from UI (dateMatch = CheckBox, fromDate&toDate = Datepicker)
bool isDateMatch = false;
dateMatch.Invoke(new MethodInvoker(delegate { isDateMatch = dateMatch.Checked; }));
DateTime fromDt = new DateTime();
fromDate.Invoke(new MethodInvoker(delegate { fromDt = fromDate.Value; }));
DateTime toDt = new DateTime();
toDate.Invoke(new MethodInvoker(delegate { toDt = toDate.Value; }));
Run Code Online (Sandbox Code Playgroud)
//example invoke usage to change UI (statusTxt = TextBox, progressBar = ProgressBar)
private void changeStatus(string statusTextV, bool enableProgressBar)
{
   statusTxt.Invoke(new MethodInvoker(delegate …
Run Code Online (Sandbox Code Playgroud)

c# invoke backgroundworker winforms

1
推荐指数
1
解决办法
58
查看次数

从流中反序列化 json 得到 null 对象

我有一个 json 文件的示例 https://pastebin.com/wL6LWbxk

{
    "rates": [{
            "code": "ATS",
            "date": "2021-03-05",
            "date_from": "2021-03-05",
            "number": 40,
            "parity": 1,
            "exchange_middle": 8.5448
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我有以下代码:

...
var responseStream = await response.Content.ReadAsStreamAsync(); //Im getting bytes OK
var something = await JsonSerializer.DeserializeAsync<Curr>(responseStream); //object something is null
...
Run Code Online (Sandbox Code Playgroud)

我的模型看起来像:

    public class Currency
    {
        [JsonProperty("code")]
        public string Code { get; set; }

        [JsonProperty("date")]
        public DateTimeOffset Date { get; set; }

        [JsonProperty("date_from")]
        public DateTimeOffset DateFrom { get; set; }

        [JsonProperty("number")]
        public long Number { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# json system.text.json

1
推荐指数
1
解决办法
565
查看次数