LinqToLDAP字符串和日期时间

本文关键字:时间 日期 字符串 LinqToLDAP | 更新日期: 2023-09-27 18:11:21

My Model:

...
[DisplayName("Birthday")]
public DateTime extensionAttribute1 { get; set; }

和控制器:

var config = new LdapConfiguration();
        config.ConfigureFactory("DOMAIN");
        using (var context = new DirectoryContext(config))
        {
            var user =
                (from u in
                     context.Query(new ADUser(), "OU=xxx,DC=xxx", "User")
                         .Where(d => d.SAmAccountName == samaccount)
                 select new ADUser()
                 {
                     SAmAccountName = u.SAmAccountName,
                     Department = u.Department,
                     DisplayName = u.DisplayName,
                     HomePhone = u.HomePhone,
                     Mail = u.Mail,
                     Mobile = u.Mobile,
                     PostalCode = u.PostalCode,
                     StreetAddress = u.StreetAddress,
                     TelephoneNumber = u.TelephoneNumber,
                     ThumbnailPhoto = u.ThumbnailPhoto,
                     Title = u.Title,
                     l = u.l,
                     PhysicalDeliveryOfficeName = u.PhysicalDeliveryOfficeName,
                     extensionAttribute1 = u.extensionAttribute1
                 }).FirstOrDefault();
            return user;
        }

给出错误:字符串未被识别为有效的日期时间

如何告诉extentionAttribute1是一个日期时间?

AD中extentionAttribute的值为"1976-08-31"

如果我将模型更改为字符串它工作得很好,但然后我的编辑器不会显示日期选择器

LinqToLDAP字符串和日期时间

您需要给格式一点提示,以便将它们解析回DateTime对象。

在你的ADUser构造函数中这样做…

 extensionAttribute1 = DateTime.ParseExact(u.extensionAttribute1, "yyyy-MM-dd",  CultureInfo.InvariantCulture);