最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Format Fluent API code from EF in Visual Studio 2022 - Stack Overflow

programmeradmin1浏览0评论

I am struggling to format code in C# in Visual Studio if it is written for the Fluent API of Entity Framework.

Something like this:

_db.ProdSmartSorts
    .Where(x => catIds.Contains((int)x.Product.CategoryId))
    .OrderBy(x => x.ProdSmartId)
    .Select(x => x.Product)
    .Include(p => p.Pictures.Where(pic => pic.IsCover))
    .Skip(prodCount * (pageNumber - 1))
    .Take(prodCount)
    .ToList();

What I want:

  • align all function calls, so each of them has the same spacing from the left
  • maybe just one function call per line
  • maybe indent .ThenInclude() further

What I have:

  • formatting code does not affect fluent code at all
  • I did not find any setting for this

Is there a (hidden) setting for this? Are there any extensions which solve this?

I am struggling to format code in C# in Visual Studio if it is written for the Fluent API of Entity Framework.

Something like this:

_db.ProdSmartSorts
    .Where(x => catIds.Contains((int)x.Product.CategoryId))
    .OrderBy(x => x.ProdSmartId)
    .Select(x => x.Product)
    .Include(p => p.Pictures.Where(pic => pic.IsCover))
    .Skip(prodCount * (pageNumber - 1))
    .Take(prodCount)
    .ToList();

What I want:

  • align all function calls, so each of them has the same spacing from the left
  • maybe just one function call per line
  • maybe indent .ThenInclude() further

What I have:

  • formatting code does not affect fluent code at all
  • I did not find any setting for this

Is there a (hidden) setting for this? Are there any extensions which solve this?

Share Improve this question edited Feb 16 at 13:45 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 16 at 12:56 Mo3biusMo3bius 6621 gold badge8 silver badges19 bronze badges
Add a comment  | 

2 Answers 2

Reset to default -1

The formatting options in Visual Studio can be found under Tools > Options > Text Editor > C# (or any other supported language), but afaik VS doesn't offer, what you want.

I use Jetbrains Resharper to add more formatting options to my ide. Unfourtunately, it is not free and has a noticable performance impact (at least when editing large files). Though it does support the requested "one dot per line" formatting ("wrap chained method calls"), all follow-up-rows are indented to the same level. Individual formatting of selected methods is not possible.

I tried the method you mentioned, and it does meet some of the requirements.

However, this method is not customizable and can only perform the functions mentioned in the documentation: Wrap call chains document

If you want better customization for the formatting you mentioned, the built-in features of VS may not provide much help.

发布评论

评论列表(0)

  1. 暂无评论