|
That will give a null reference exception if p.TaxClienteID is not found in clients.
Truth,
James
|
|
|
|
|
Yep.
I'm not doing all his work for him!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
This should be the "proper" version of what you want:
void Main()
{
var originDocument = new List<FinalDocument>();
var clients = new List<Client>();
List<FinalDocument> finalDocument =
(from p in originDocument
join c in clients on p.TaxClienteID equals c.TaxClienteID
select new FinalDocument
{
documentcode = p.documentcode,
date = p.date,
codcliente = c.codcliente
}).ToList();
}
class FinalDocument
{
public string documentcode { get; set; }
public DateTime date { get; set; }
public string codcliente { get; set; }
public string TaxClienteID { get; set; }
}
class Client
{
public string codcliente { get; set; }
public string TaxClienteID { get; set; }
}
Truth,
James
|
|
|
|
|
Thanks James, this code was very useful
Regards
Rui
|
|
|
|
|
hi all,
i got this error
<br />
Mixed mode assembly is built against version 'v2.0.50727' of runtime and cannot be loaded in the 4.0 runtime without additional configuration.<br />
i changed my app.config file but still this occur.
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<!-- 4.0 RTM -->
<supportedRuntime version="v4.0.30319"/>
<!-- 2.0 RTM -->
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
please help for this.
|
|
|
|
|
Try adding this to app.config:
<startup useLegacyV2RuntimeActivationPolicy="true" />
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Of course since Microsoft got rid of DLL hell this is so easy.
|
|
|
|
|
i already use it, and mentioned it above
|
|
|
|
|
That attribute is the solution to the error you're getting. Are you sure you've updated the correct config file?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
(1) does this block on the first item's await or will await on all?
(2) if the latter, what's the implication if the wrapped http client in
GetItemChildAsync is a singleton?
foreach (var i in items)
{
await GetItemChildAsync(i.Id);
}
|
|
|
|
|
1) Technically, the whole point of await is that it doesn't "block". It simply creates a continuation callback representing the rest of the method, and passes that to the task being awaited.
However, from a logical perspective, the rest of the method is "blocked" until the task returned from GetItemChildAsync completes. That happens when the task is awaited, so it "blocks" on the first item.
If you wanted to start all of the tasks at the same time, and then wait for them all to complete, you would use the Task.WhenAll method[^]:
IEnumerable<Task> tasks = items.Select(i => GetItemChildAsync(i.Id));
await Task.WhenAll(tasks);
2) None whatsoever. The HttpClient class[^] is safe to call from multiple threads at the same time. In fact, a singleton used to be the recommended way to use this class[^]. However, the current recommendation is to use the IHttpClientFactory service[^] to create an HttpClient instance on demand.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I want to convert dates come from database. I have the following API action:
[HttpGet]
public async Task<IActionResult> GetSpecialPostsList()
{
var posts = await myContext.Posts.Where(x => x.IsSpecialPost == true && x.IsActive == true).ToListAsync();
if (posts != null)
{
var revisedPost = new RevisedPost();
var revisedPostList = new List<RevisedPost>();
foreach (var post in posts)
{
revisedPost.CreationDate = DateConverter.GregToPersianDate(post.CreationDate.ToShortDateString());
revisedPost.Description = post.Description;
revisedPost.ImageFileName = post.ImageFileName;
revisedPost.IsActive = post.IsActive;
revisedPost.IsMainPost = post.IsMainPost;
revisedPost.IsSpecialPost = post.IsSpecialPost;
revisedPost.Title = post.Title;
revisedPost.Id = post.Id;
revisedPostList.Add(revisedPost);
}
return Ok(revisedPostList);
}
return NotFound("Data not found!");
}
The problem is that when new data is added to the list, all the items in the list are updated to the latest item values. This means all the items in the list are the same.
What is wrong with my code?
|
|
|
|
|
You keep updating and adding the "same" revisedPost object in your for loop (instead of creating a new instance). "Adding" does not make "copies" of "reference" (i.e. class) objects.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Thanks. I put the following code inside the loop and the problem solved.
var revisedPost = new RevisedPost();
|
|
|
|
|
In your send line, "posts" will also be non-null, so that if() will always be true. It should be if(posts.Any())
Truth,
James
|
|
|
|
|
I need to be able to send text to a selected worksheet that is already opened.
This, I think is close but it didn't work:
<pre lang="C#">
string wb = cmb_BookName.Text.ToString();
string ws = cmb_SheetName.Text.ToString();
if (chkContainer.Checked)
{
Excel.Application oexcel = new Excel.Application();
Excel.Workbook wkbk = (Excel.Workbook)oexcel.Workbooks[wb];
Excel.Worksheet wksk = (Excel.Worksheet)wkbk.Sheets[ws];
Range cellRange = wksk.Range["D48:D48"];
cellRange.Value = cboContainer.Text;
}
</pre>
The code builds without errors but when running it stops at the line trying to get the workbook and gives me an Exception Unhandled message.
So basically my question still is how do I work with an excel workbook that is already opened?
Seems like most of the articles that I find are opening an excel file and then working with it. In my case, I need to work with an already opened excel workbook.
|
|
|
|
|
You can't, unless it is a shared workbook. Excel locks files when you open them, to prevent changes affecting what you are doing.
Have a look here: c# - Is posible edit opened excel file - Stack Overflow[^]
But to be honest, if you need multiuser access to a file, you'd be a lot better off moving to a SQL Server / MySQL database as they are designed for that! Excel is a spreadsheet - and a damn good one - but it is not a DB, and shouldn't be used for one for anything beyond the trivial.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
If the workbook is already opened in the same user session, you need to use Marshal.GetActiveObject[^] to get the running Excel instance, rather than starting a new instance.
Excel.Application oexcel = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
if (oexcel is null) throw new InvalidOperationException("Excel is not running.");
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Interopping through Excel.
In general, open files cannot be written to. This interop is a cheat, but with a big bottleneck and won't scale.
You might need an actual developer who can design a database, and those are expensive
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I'll venture this: msdocs[^] ... since noone has yet made a similar citation.
|
|
|
|
|
RedDk wrote: since noone has yet made a similar citation
Who is "noone"?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
Any malenky malchick az dim a citizen such as yourself might encounter at the mestos bar. I suppose.
modified 21-May-22 14:03pm.
|
|
|
|
|
Hello everybody, I pretty new to programming, what I want to do is a C# program that will send a message to a Microsoft teams group via a webhook connector of this group.
I was able to achieve it with a Desktop code(with form),but I need it to be a console program, I have pretty much copy/paste the code from my Desktop project to my console project, but there something that don't work. Here is my code from my desktop project:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test_team_2
{
public partial class Form1 : Form
{
string[] cmdLineMe = Environment.GetCommandLineArgs();
string webhookPdfTermine = "uri to my team groups;
HttpClient client = new HttpClient();
Message body = new Message();
string finalMessage = "";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < cmdLineMe.Length; i++)
{
finalMessage = cmdLineMe[i];
}
sendMsg();
}
public class Message
{
public string text { get; set; }
}
private async Task SendMsgAsync()
{
MessageBox.Show(finalMessage);
client.BaseAddress = new Uri(webhookPdfTermine);
body.text = finalMessage;
string serializeJson = JsonSerializer.Serialize(body);
StringContent content = new StringContent(serializeJson, Encoding.UTF8, "application/json");
_ = await client.PostAsync(client.BaseAddress, content);
}
void sendMsg()
{
_ = SendMsgAsync();
}
}
}
And here is my code for my console project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
namespace TeamBots
{
class Program
{
string[] cmdLineMe = Environment.GetCommandLineArgs();
string webhookPdfTermine = "uri to my team groups";
HttpClient client = new HttpClient();
Message body = new Message();
string finalMessage = "";
static void Main(string[] args)
{
Program myprog = new Program();
for (int i = 0; i < myprog.cmdLineMe.Length; i++)
{
myprog.finalMessage = myprog.cmdLineMe[i];
}
myprog.sendMsg();
}
private async Task SendMsgAsync()
{
Console.WriteLine(finalMessage);
client.BaseAddress = new Uri(webhookPdfTermine);
body.text = finalMessage;
string serializeJson = JsonSerializer.Serialize(body);
StringContent content = new StringContent(serializeJson, Encoding.UTF8, "application/json");
_ = await client.PostAsync(client.BaseAddress, content);
}
void sendMsg()
{
_ = SendMsgAsync();
}
public class Message
{
public string text { get; set; }
}
}
}
Like I said before, I pretty new to programming, the code was taken from website there and there...
If anyone can help it will be very appreciated.
Thank you
modified 18-May-22 15:09pm.
|
|
|
|