当前位置: 首页 > 工具软件 > Go-LINQ > 使用案例 >

工作之杂记--first linq

韦鸣
2023-12-01

使用linq:查询赋予自定义对象

string _eventName = Request["Content"] ?? "";
            var _userComment = from com in db.UserComment
                               join users in db.User on com.UserID equals users.Id
                               //join hd in db.table1 on com.EventID equals hd.ID
                               //where hd.HuoDongName.Contains(_eventName)
                               select new UserComments
                               {
                                   ID = com.ID,
                                   EventID = com.EventID,
                                   UserID = com.UserID,
                                   EventContent = com.EventContent,
                                   UpQty = com.UpQty,
                                   DownQty = com.DownQty,
                                   InDateTime = com.InDateTime,
                                   UserName = users.UserName//,hd.HuoDongName
                               };
            List<UserComments> comm = _userComment.ToList();

注意点:自定义对象一定要有构造函数 如下
    public class UserComments
    {
        public int ID { get; set; }
        public int Event { get; set; }
        //.....
        public UserComments(int Id, int eventID)
        {
            this.ID = Id;
            this.Event = eventID;
        }
    }

go on

                 var _even = from hd in db.table1
                            orderby hd.ID descending
                            select new NameValue { Value = hd.ID, Name = hd.HuoDongName };

                huoDongList = _even.ToList();

                var _user = from user in User
                            where user.Status==1
                            select new NameValue { Value = user.Id, Name = user.NickName };

                userList = _user.ToList();


 类似资料: