简体   繁体   English

c#使用ViewModel的泛型类型的函数

[英]c# use a function on a generic type with ViewModel

Is this possible? 这可能吗? I am trying to avoid a lot of copying and pasting from area to area. 我试图避免从一个区域到另一个区域的大量复制和粘贴。 I have a search function (I have reduced the code for simplicity). 我有一个搜索功能(为简单起见,我减少了代码)。

if (!String.IsNullOrEmpty(filterVM.searchString))
            {
                var nameSearch = filterVM.searchString.ToLower();
                guests = guests.Where(g => g.FirstName.ToLower().StartsWith(nameSearch)
                || g.LastName.ToLower().StartsWith(nameSearch)
                )
            }

            filterVM.FilteredResultsCount = guests.CountAsync();

Guests can change from area to area, but it always has the same base things, like FirstName and LastName, ex: 客人可以从一个区域更改为区域,但它始终具有相同的基本内容,例如FirstName和LastName,例如:

public class GuestBasicBase
    {

        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public string GuestGuid { get; set; } = Guid.NewGuid().ToString();

        public string FirstName { get; set; }

        public string LastName { get; set; }
}

Then I can have a bigger class for a particular area like 然后,我可以为特定区域设置更大的课程

 public class AreaOneGuest : GuestBasicBase
    {
        public int ID {get; set;}
        public string ExtraFieldOne { get; set; }    
        public string ExtraFieldTwo { get; set; }    
//Etc    
        }

I would like to have a function which will return a viewmodel and part of that viewmodel is PaginatedList and the other part is the Filter parameters, like this: 我想有一个函数将返回一个viewmodel,该viewmodel的一部分是PaginatedList,另一部分是Filter参数,如下所示:

public class GuestBasicBaseIndexVM
    {
        public PaginatedList<T:GuestBasicBase> Guests  { get; set; } 

        public GuestIndexFilterVM FilterVM { get; set; }
    }

And I want a function to return this but take in a larger field, like 我想要一个函数来返回它,但是需要一个更大的领域,比如

public async Task<GuestBasicBaseIndexVM>(T:GuestBasicBase, GuestIndexFilterVM filterVM){
//do search function
return (T where T: GuestBasicBase)
}

Does this question make sense and is it possible? 这个问题是否有意义,是否可能? Currently trying on my own and seeing what happens...I feel like it is sort of like the PaginatedList class but I am not certain 目前正在尝试自己,看看会发生什么......我觉得它有点像PaginatedList类,但我不确定

Not exactly what I wanted, but here is what I did. 不完全是我想要的,但这就是我所做的。 changed my BaseViewModel like this: 像这样改变了我的BaseViewModel:

    public class GuestBasicBaseIndexVM
        {
            public IEnumerable<GuestBasicBase> Guests  { get; set; } 
            //Changed from a PaginatedList

            public GuestIndexFilterVM FilterVM { get; set; }
        }

and this function: 而这个功能:

public static async Task<GuestBasicBaseIndexVM> CreateUpdatedGuestList(GuestIndexFilterVM filterVM, IQueryable<GuestBasicBase> guests)
{
//Code to search through guests and return filtered list and filters viewmodel
}

Then after the Ienumaerable of basic guests is returned I did this to connect them back to the AreaOne Guests 然后在基本客人的Ienumaerable返回后,我这样做将他们连接回AreaOne客人

var x = await Helpers.CreateUpdatedGuestList(filterVM, guests);
var hsIDs = x.Guests.Select(v => v.GuestGuid).ToHashSet(); //Filtered GuestGuids to hashset

areaOneGuests = guests.Where(g => hsIDs.Contains(g.GuestGuid)) //This matches up the filtered list of base guests to the actual full guests.
//Then whatever code to do what I want with the AreaOne Guests....

It wasn't exactly what I was trying to do, but still saves me a lot of copying and pasting from area to area with similar base Guest classes. 这不是我想要做的,但仍然节省了大量的复制和粘贴区域与类似的基础Guest类。 Have not been able to measure any noticeable performance loss/gain doing it this way. 无法通过这种方式测量任何明显的性能损失/增益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM