I need to get a linq querry for custom class MyFileData
public class MyFileData
{
private string name;
private string last_mod;
private string file_type;
private long size;
public string Name
{
get { return name; }
set { name = value; }
}
public string LastMod
{
get { return last_mod; }
set { last_mod = value; }
}
public long Size
{
get { return size; }
set { size = value; }
}
public string FileType
{
get { return file_type;}
set { file_type = value; }
}
}
but I want as an output values of all properties without file_type like this
from file in files
select new MyFileData
{
Name = file.Name,
LastMod = file.LastMod,
Size = file.Size,
};
so the output without the "fileType"
[{"name":"desktop.ini","lastMod":"07.12.2019 10:12:42","size":174,"fileType":null}]
in this way
[{"name":"desktop.ini","lastMod":"07.12.2019 10:12:42","size":174}]
also I would like to group the files with the type of the extenstion like that
from file in files
group file by file.FileType;
but it gives an error.
The files is IEnumerable<MyFileData>
type.
CodePudding user response:
In a simple way, you can try to use an anonymous class
files.Select(x=> new {
Name = x.Name,
LastMod = x.LastMod,
Size = x.Size
});
but I would create another class that carries you expect property as a ViewModel
.
CodePudding user response:
You could try to set null value handling in the JsonProperty
attribute. Setting NullValueHandling = NullValueHandling.Ignore
makes null values ignored when serializing or deserializing objects.
[JsonProperty("fileType", NullValueHandling = NullValueHandling.Ignore)]
public string FileType
{
get { return file_type;}
set { file_type = value; }
}