简化使用多级 Select 子查询的 Linq 查询。

huangapple go评论95阅读模式
英文:

Simplify Linq query with Select subquery multiple levels

问题

我有这个Linq查询。有人可以帮我简化一下吗?我正在尽可能提高性能。我正在使用第三方API和一个经过筛选的字段列表的数据库。Field是我们代码中的类,其他都是OnBase Hyland对象。FilteredFields列表是需要迭代的数据的主列表。

var keywordTypeList = app.Core.KeywordTypes;
var applicationList = app.WorkView.Applications;
var datasetList = app.WorkView.Datasets;
var fieldsList = new List<Models.Field>();
var fieldAsAttribute =
    from filteredField in filteredFields.ToList()
    join application in applicationList on filteredField.wv_app equals application.Name
    select new
    {
        aclassQuery = (
            from aclass in applicationClasses
            where aclass.Name == filteredField.wv_class && application.Name == flteredField.wv_app
            select new
            {
                attributeQuery = (
                    from attribute in aclass.Attributes
                    where attribute.Name == filteredField.wv_attr
                    select new Models.Field
                    {
                        FieldId = filteredField.field_id,
                        Name = filteredField.field_name,
                        HasDropdown = true,
                        DropdownId = application.Filters.Where(a => a.Name == filteredField.wv_filter).SingleOrDefault().ID,
                        DropdownType = Models.Field.DropdownTypeEnum.FilterEnum
                    }
                )
            })
    };

fieldsList1.AddRange(fieldAsAttribute.ToList()); //Doesn't work

foreach(var one in fieldAsAttribute)
{
    foreach(var two in one.aclassQuery)
    {
        foreach(var three in two.attributeQuery)
        {

        }
    }
}

类定义

public partial class field
{
    public long field_id { get; set; } 
    public string field_name { get; set; }
    public bool kw_flag { get; set; }
    public string kw_name { get; set; }
    public bool wv_flag { get; set; }
    public string wv_app { get; set; }
    public string wv_class { get; set; }
    public string wv_attr { get; set; }
    public string wv_dataset_name { get; set; }
    public string wv_filter_app { get; set; }
}
var fieldsList = new List<Models.Field>();
public KeywordTypeList KeywordTypes => _keywordTypeList ?? (_keywordTypeList = new KeywordTypeListImplementation(base.Application));
public ApplicationList Applications => _applications ?? (_applications = new ApplicationListImplementation(base.Application));
public class Application 
{     
    public ClassList Classes;

    public FilterList Filters;

    public long ID { get; internal set; }

    public string Name { get; internal set; }
}


public class Class 
{
    public AttributeList Attributes;

    public long ID { get; internal set; }

    public string Name { get; internal set; }

    internal Application WVApplication { get; set; }
}

public sealed class Attribute  
{
    public Class Class => base.Application.WorkView.GetClassByID(ClassID);

    internal long ClassID { get; set; }
}
英文:

I have this Linq query. Can anyone help me to simplify it ? I am trying to make this as performance effective as possible. I am working against a third party api and a DB list of filtered fields.
Field is our Class in our code. Everything else is a OnBase Hyland object.
FilteredFields List is the master list of data that need to iterating it.

var keywordTypeList = app.Core.KeywordTypes;
var applicationList = app.WorkView.Applications;
var datasetList = app.WorkView.Datasets;
var fieldsList = new List&lt;Models.Field&gt;();
var fieldAsAttribute =
    from filteredField in filteredFields.ToList()
    join application in applicationList on filteredField.wv_app equals application.Name
    select new
    {
        aclassQuery = (
            from aclass in applicationClasses
            where aclass.Name == filteredField.wv_class &amp;&amp; application.Name == flteredField.wv_app
            select new
            {

                attributeQuery = (
                    from attribute in aclass.Attributes
                    where attribute.Name == filteredField.wv_attr
                    select new Models.Field
                    {
                        FieldId = filteredField.field_id,
                        Name = filteredField.field_name,
                        HasDropdown = true,
                        DropdownId = application.Filters.Where(a =&gt; a.Name == filteredField.wv_filter).SingleOrDefault().ID,
                        DropdownType = Models.Field.DropdownTypeEnum.FilterEnum
                    }
                )
            })
    };

    fieldsList1.AddRange(fieldAsAttribute.ToList()); //Doesn&#39;t work

    foreach(var one in fieldAsAttribute)
    {
        foreach(var two in one.aclassQuery)
        {
            foreach(var three in two.attributeQuery)
            {

            }
        }
    }
```

Class definitions

public partial class field
{
     public long field_id { get; set; } 
     public string field_name { get; set; }
     public bool kw_flag { get; set; }
     public string kw_name { get; set; }
     public bool wv_flag { get; set; }
     public string wv_app { get; set; }
     public string wv_class { get; set; }
     public string wv_attr { get; set; }
     public string wv_dataset_name { get; set; }
     public string wv_filter_app { get; set; }
}
var fieldsList = new List&lt;Models.Field&gt;();
 public KeywordTypeList KeywordTypes =&gt; _keywordTypeList ?? (_keywordTypeList = new KeywordTypeListImplementation(base.Application));
 public ApplicationList Applications =&gt; _applications ?? (_applications = new ApplicationListImplementation(base.Application));
public  class Application 
{     
    public ClassList Classes;

    public FilterList Filters;

    public long ID { get; internal set; }

    public string Name { get; internal set; }
}


public class Class 
{
    public AttributeList Attributes;

    public long ID { get; internal set; }

    public string Name { get; internal set; }

    internal Application WVApplication { get; set; }
}

public sealed class Attribute  
{
    public Class Class =&gt; base.Application.WorkView.GetClassByID(ClassID);

    internal long ClassID { get; set; }

        
 }

答案1

得分: 1

请尝试以下查询:

var fieldsQuery =
    from filteredField in filteredFields
    join application in applicationList on filteredField.wv_app equals application.Name
    join aclass in applicationClasses on 
        new { filteredField.wv_class, flteredField.wv_app } 
        equals 
        new { wv_class = aclass.Name, wv_app = application.Name } 
    from attribute in aclass.Attributes
        .Where(attribute => filteredField.wv_attr == attribute.Name)
    from filter in application.Filters
        .Where(filter => filteredField.wv_filter == filter.Name)
        .Take(1)
        .DefaultIfEmpty()
    select new Models.Field
    {
        FieldId = filteredField.field_id,
        Name = filteredField.field_name,
        HasDropdown = true,
        DropdownId = filter?.ID,
        DropdownType = Models.Field.DropdownTypeEnum.FilterEnum
    };

var fieldsList = fieldsQuery.ToList();
英文:

Try the following query:

var fieldsQuery =
    from filteredField in filteredFields
    join application in applicationList on filteredField.wv_app equals application.Name
    join aclass in applicationClasses on 
        new { filteredField.wv_class, flteredField.wv_app } 
        equals 
        new { wv_class = aclass.Name, wv_app = application.Name } 
    from attribute in aclass.Attributes
        .Where(attribute =&gt;  filteredField.wv_attr == attribute.Name)
    from filter in application.Filters
        .Where(filter =&gt; filteredField.wv_filter == filter.Name)
        .Take(1)
        .DefaultIfEmpty()
    select new Models.Field
    {
        FieldId = filteredField.field_id,
        Name = filteredField.field_name,
        HasDropdown = true,
        DropdownId = filter?.ID,
        DropdownType = Models.Field.DropdownTypeEnum.FilterEnum
    };

var fieldsList = fieldsQuery.ToList();

huangapple
  • 本文由 发表于 2023年8月9日 10:27:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定