且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将TSQL转换为Linq到实体

更新时间:2023-01-16 18:49:58

这应该给你正确的结果: -

  var result = from f in _dbContext.FileLists 
group f by f.Extension into g
let firstCategory = _dbContext .ExtensionsCategories
.FirstOrDefault(x => x.Extension == g.Key)
选择新
{
类别= firstCategory!= null? firstCategory.Category:,
TotalFileCount = g.Count()
};

或者如果需要方法语法 -

  var result = _dbContext.FileLists.GroupBy(e => e.Extension)
.Select(x =>
{
var firstCategory = _dbContext.ExtensionsCategories
.FirstOrDefault(z => z.Extension == x.Key);
return new
{
Category = firstCategory!= null?firstCategory.Category:,
TotalFileCount = x.Count()
};
});

小提琴与Linq对象。



更新:



如果一个类别可以包含多个扩展名,那么可以使用下面的查询: -

  var result = extensionCat.GroupBy(x => ; x.Category)
.Select(x =>
new
{
Category = x.Key,
TotalFileCount = fileList.Count(f => x
.Any(z => z.Extension == f.Extension))
}
);


I have 2 table named FileList and ExtensionsCategory :

FileList
-------------------------------------------------------------
ID    Path         Extension

1     C:\7.pdf        pdf
2     D:\3.png        png
3     C:\1.mp3        mp3
4     D:\32.pdf       pdf

and

ExtensionsCategory 
------------------------------------------------------------
ID     Extension       Category

1        mp3           Multimedia
2        pdf           Document
3        png           Photo

there isn't any relation between tables.

now i want to know each category contain how many files ? i wrote the following TSQL query in SSMS and work fine :

SELECT 
     Category
    ,SUM(FileCount) AS TotalFileCount 
FROM    
    (SELECT 
         COUNT(*) AS FileCount
        ,(SELECT ExtensionsCategories.Category FROM ExtensionsCategories WHERE FileList.Extension = ExtensionsCategories.Extension) AS Category
    FROM FileList GROUP BY Extension) AS T1 GROUP BY Category

i want know is there any improved way to do this without Joins ?

i try to write it using Linq to Entities, this is my code but i can't complete it :

    var a = _dbContext.FileLists
        .Select(fl => new
        {
            Extension = fl.Extension,
            Count = _dbContext.FileLists.Count(),
            Cat =
                _dbContext.ExtensionsCategories.Where(w => w.Extension == fl.Extension).Select(s => s.Category)
        }).GroupBy(g => g.Extension);

This should give you correct result:-

var result = from f in _dbContext.FileLists
             group f by f.Extension into g
             let firstCategory = _dbContext.ExtensionsCategories
                                           .FirstOrDefault(x => x.Extension == g.Key)
             select new
                    {
                        Category = firstCategory != null ? firstCategory.Category : "",
                        TotalFileCount = g.Count()
                    };

Or if you need with Method Syntax:-

var result = _dbContext.FileLists.GroupBy(e => e.Extension)
              .Select(x =>
                  {
                      var firstCategory = _dbContext.ExtensionsCategories
                                           .FirstOrDefault(z => z.Extension == x.Key);
                      return new
                        {
                           Category = firstCategory != null ? firstCategory.Category : "",
                           TotalFileCount = x.Count()
                        };
                  });

Fiddle with Linq-to-objects.

Update:

If a category can contain multiple extension then you can use below query:-

var result = extensionCat.GroupBy(x => x.Category)
                      .Select(x =>
                         new
                          {
                              Category = x.Key,
                              TotalFileCount = fileList.Count(f => x
                                                    .Any(z => z.Extension == f.Extension))
                          }
                      );