This question already has answers here:
Remove file extension from a file name string
(13 answers)
Closed 4 years ago.
I have an array with items having names with .txt.
I want to remove the extension. The following code is not working.
var xx = filenames.ForEach(x =>
{
int fileExtPos = x.LastIndexOf(".");
if (fileExtPos >= 0)
x = x.Substring(0, fileExtPos);
});
Can anyone help what am i doing wrong here?
Thanks
There is a build in method GetFileNameWithoutExtension() which could be used. That's the common way to handle this.
var result = filenames.Select(System.IO.Path.GetFileNameWithoutExtension);
You error appears here var xx = filenames.ForEach because ForEach has no return value (void) which could be assigned to var xx.
Related
This question already has answers here:
Best practice to check if DataRow contains a certain column
(6 answers)
Closed 13 days ago.
in the below code, I need to check whether the DBMSType exists or not.
string filename = #"D:\service\configfile.json";
DataSet data = JsonConvert.DeserializeObject<DataSet>(File.ReadAllText(filename));
if (data.Tables[0].Rows.Count > 0)
{
var n = data.Tables[0].Rows[0]["DBMSType"].ToString();
}
You can try:
if (data.Tables[0].Rows.Count > 0 && !string.IsNullOrWhiteSpace(data.Tables[0].Rows[0]["DBMSType"]?.ToString()))
{
var n = data.Tables[0].Rows[0]["DBMSType"].ToString();
...
}
this is the solution got working for me
so just try the below validation it is working fine
data.Tables[0].Columns.Contains("DBMSType")
This question already has answers here:
Convert String to Type in C# [duplicate]
(4 answers)
Closed 1 year ago.
I would like to add columns to a existing DataTable.
However, the name of the new column and the type, came from a string list.
ColumnName_list = FieldX, FieldY ...
ColumnType_list = Sistem.String, System.Decimal ...
The order is always related between the two lists.
I am trying to use the code below, however is getting type error conversion
t = 0;
foreach (var name in ColumnName_list)
{
dt1.Columns.Add(name, ColumnType_list[t]);
t++;
}
Compiler error at line 27: cannot convert from 'string' to 'System.Type'
Where Dt1 is the Datatable.
Thanks and regards.
May you try
t = 0;
foreach (var name in ColumnName_list)
{
dt1.Columns.Add(name, typeof(ColumnType_list[t]) );
t++;
}
May you share the result after you try
Have a nice day
This question already has answers here:
count objects of a certain type in a collection and use this as a string in a textbox
(1 answer)
Finding count of particular items from a list
(5 answers)
Get Count in List of instances contained in a string
(6 answers)
Get item count of a list<> using Linq
(3 answers)
count objects that meet certain condition in List-collection
(2 answers)
Closed 5 years ago.
I have a ReadOnlyCollection of a Smartcard type from Microsoft.Clm.Shared.Smartcards namespace.
one of the fields/parameters of the smartcard object is AssingnedUserName.
I need to be able to count how many times a smartcard with the same username exist in the list,
something like:
[Pseudo Code]
int count = (smartcardCollection.AssignedUserName == my String).Count().
I tried to use the ReadOnlyCollection.Tolist() method, but I couldn't find the correct syntax to make it work.
I also found many examples but non for a ReadOnlyCollection object !
what is the best practice for achieving this ?
thanks
David.
just use this
int count = smartcardCollection.Count(s=>s.AssignedUserName == my String);
LINQ Count it takes a function to test each element for a condition
You just need to use the overload of Count or Where ... Count:
int count = smartcardCollection.Count(s => s.AssignedUserName == my String);
or
int count = smartcardCollection.Where(s => s.AssignedUserName == my String).Count();
This question already has answers here:
Is string in array?
(10 answers)
Closed 6 years ago.
For example
foreach(var content in Model.Where(c=>c.Key != array))
Where array is
string[] = new string[] {"KeyName1", "KeyName2", "KeyName3"};
This would be immensely helpful as I have almost 20 Keynames, and would like an easier way to manage them than using a long list of
c.Key != "KeyName1" && c.Key != "KeyName2"
This is a little bit more advanced usage than I'm used to so assistance would be helpful.
foreach (var content in Model.Where(c => !array.Contains(c.Key)))
This question already has answers here:
C# List of objects, how do I get the sum of a property
(4 answers)
Closed 5 years ago.
I have the following: List<OutputRow> which contains a number of OutputRow objects.
I am wondering if there is a way for me to use a lambda function on the list to return the total sum of the values of a certain propertyX on each OutputRow object in the list.
Example list:
OutputRow.propertyX = 4
OutputRow.propertyX = 6
OutputRow.propertyX = 5
return 15
Test data
var ls=new List<OutputRow>();
ls.Add(new OutputRow(){propertyX=4});
ls.Add(new OutputRow(){propertyX=6});
ls.Add(new OutputRow(){propertyX=5});
Lambda
var total= ls.Sum(x=>x.propertyX);
SOmething like this:
var yourSum = yourOutputRowList.Sum(x => x.propertyX);