【问题标题】:C# exclude duplicate code from two method returning expressionC#从两个方法返回表达式中排除重复代码
【发布时间】:2017-11-04 14:35:01
【问题描述】:

我有两个函数,返回翻译成 EF 的表达式。:

 public static Expression<Func<TripLanguage, TripViewModel>> ToSearchModel(ILookup<int, TagViewModel> tags)
    {            
        return tripLanguage => new TripViewModel()
        {
            From = tripLanguage.From,
            To = tripLanguage.To,
            Annotation = tripLanguage.Description.Truncate(Strings.TRUNCATE_ANOTATION),
            Level = tripLanguage.Trip.Level,
            BicycleType = tripLanguage.Trip.BicycleType,
            UrlId = tripLanguage.UrlId,
            Distance = tripLanguage.Trip.Distance,
            Tags = tags[tripLanguage.TripId], //This is only different and in function args of course
            MainImage = tripLanguage.Trip.Images.OrderBy(s => s.Date).Select(i => new ImageViewModel
            {
                Filename = i.Filename,
                Id = i.Id,
                Title = i.Title
            }).Take(1)
        };
    }

    public static Expression<Func<TripLanguage, TripViewModel>> ToSearchModel()
    {
        return tripLanguage => new TripViewModel()
        {
            From = tripLanguage.From,
            To = tripLanguage.To,
            Annotation = tripLanguage.Description.Truncate(Strings.TRUNCATE_ANOTATION),
            Level = tripLanguage.Trip.Level,
            BicycleType = tripLanguage.Trip.BicycleType,
            UrlId = tripLanguage.UrlId,
            Distance = tripLanguage.Trip.Distance,                
            MainImage = tripLanguage.Trip.Images.OrderBy(s => s.Date).Select(i => new ImageViewModel
            {
                Filename = i.Filename,
                Id = i.Id,
                Title = i.Title
            }).Take(1)
        };
    }

唯一不同的是Tags 集合。是否有可能像调用方法一样,不带参数并添加排除重复代码的标签属性?还是使用一些继承表达式?

感谢您的宝贵时间。

【问题讨论】:

  • 调用第二个方法并在返回的对象上填充 Tags 属性。无需在对象初始化列表中单独使用属性设置器。
  • 您确定您的第一个查询(带有标签)已正确转换为 SQL 查询,而不是在内存中执行?
  • @Evk 是的,这是真的。这会创建 IQueryable 对象,并且在调用例如 ToList() 之后当然会执行查询。 ToSearchModel 在 Select 函数中被调用。但如果可能的话,我仍然需要删除重复的代码。
  • 不,我理解这通常是如何工作的,我只是不确定它如何将Tags = tags[tripLanguage.TripId] 转换为 SQL。
  • 这很复杂。我的观点是:您的标签最有可能导致在客户端进行评估。如果这是真的 - 很可能你对此并不满意。然后你必须稍后设置标签 - 在你的查询被执行和具体化之后(所以在ToList() 调用之后),你的问题将不再相关。

标签: c# .net asp.net-mvc linq asp.net-core


【解决方案1】:

将第一种方法更改为使用null 标签,并为其提供null 默认值:

public static Expression<Func<TripLanguage,TripViewModel>> ToSearchModel(ILookup<int, TagViewModel> tags = null) {
    return tripLanguage => new TripViewModel() {
        From = tripLanguage.From,
        To = tripLanguage.To,
        Annotation = tripLanguage.Description.Truncate(Strings.TRUNCATE_ANOTATION),
        Level = tripLanguage.Trip.Level,
        BicycleType = tripLanguage.Trip.BicycleType,
        UrlId = tripLanguage.UrlId,
        Distance = tripLanguage.Trip.Distance,
        Tags = tags?[tripLanguage.TripId], // <<== Note the question mark
        MainImage = tripLanguage.Trip.Images.OrderBy(s => s.Date).Select(i => new ImageViewModel
        {
            Filename = i.Filename,
            Id = i.Id,
            Title = i.Title
        }).Take(1)
    };
}

【讨论】:

    猜你喜欢
    • 2019-07-29
    • 2019-01-27
    • 2013-01-04
    • 2014-07-30
    • 2014-04-04
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    相关资源
    最近更新 更多