Pass in an Expression to linq's Select

前端 未结 4 1755
甜味超标
甜味超标 2021-02-13 02:07

This is linq-to-sql

I have a lot of different classes all doing the same query, but projecting the results slightly differently. Ideally I\'d like to b

4条回答
  •  隐瞒了意图╮
    2021-02-13 03:01

    If I understand your question correctly you can use this code:

    first declare a method for selecting your data like this:

    public List FindAll(Func selector) where TResult : class
        {
            using (RepositoryDataContext = new DataClasses1DataContext())
            {
                    return RepositoryDataContext.Regions.Select(selector).ToList();
    
            }
        }
    

    then you can build your select statement like this:

    Func select = r => new SelectAllRegion
            {
                RegionID = r.RegionID,
                RegionDescription = r.RegionDescription
            };
    

    my SelectAllRegion :

     public class SelectAllRegion
    {
        public SelectAllRegion()
        {
        }
        public int RegionID { get; set; }
        public string RegionDescription { get; set; }
    }
    

    and region is Region table in northwing.I hope this help you

提交回复
热议问题