1

I need to get a linq query 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 extension like that

from file in files
group file by file.FileType;

but I get an error.

The files is IEnumerable<MyFileData> type.

Below whole endpoint map for the program:

app.MapGet($"/browse", (string? path, string? group, MyGetFilesInfo myGetFilesInfo) =>
{
IEnumerable<MyFileData> files = myGetFilesInfo.GetFiles(path);

if (group == "true")
{
    return
        from file in files
        select file;
}

var query = 
    from file in files
    select new MyFileData
    {
        Name = file.Name,
        LastMod = file.LastMod,
        Size = file.Size,
    };

return JsonConvert.SerializeObject(query);
});
3
  • 2
    Please add the exact error message. Commented Mar 21, 2022 at 12:42
  • CS1593 Delegate 'RequestDelegate' does not take 3 arguments Commented Mar 21, 2022 at 12:43
  • It's in case of trying to use group ... by , considering only the first part, the program compiles, but as mentioned the output is the whole MyFileData object with all properties Commented Mar 21, 2022 at 12:45

2 Answers 2

2

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
});

Edit

The error is caused because your endpoint returns two different types that will let the compiler confuse.

You can try to use Results.Json method Unite that return your JSON string.

app.MapGet($"/browse", (string? path, string? group, MyGetFilesInfo myGetFilesInfo) =>
{
    IEnumerable<MyFileData> files = myGetFilesInfo.GetFiles(path);

    if (group == "true")
    {
        return Results.Json(files);
    }

    var query = 
        from file in files
        select new 
        {
            Name = file.Name,
            LastMod = file.LastMod,
            Size = file.Size
        };

    return Results.Json(query);
});
Sign up to request clarification or add additional context in comments.

7 Comments

I tried with this, but then the CS1593 also occurs
@KubaJ I edit my answer as lambda you can try it
Even with lambda it gives CS1593
@KubaJ Could you provide your full code of files? your error seem like be another thing.
I've added it to the main question
|
1

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; }
}

6 Comments

I tried adding attribute but it still returns the fileType property
Could you post the code you use to create the JSON string, please?
JObject querry = new JObject( new JArray( from file in files select new JObject( new JProperty("name", file.Name), new JProperty("lastMod", file.LastMod), new JProperty("size", file.Size))) );
For the JsonProperty attribute to work, you would need to use JsonConvert.SerializeObject instead of manually created JObject.
so I've tried also with Serializing object (shown in the end of the main question), but it still does not work with the same error
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.