LinqToLDAP字符串和DateTime

时间:2014-07-03 12:28:41

标签: c# linq razor ldap

我的模特:

...
[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;
        }

给出错误:字符串未被识别为有效的DateTime

如何判断extentionAttribute1是DateTime?

在AD中,extentionAttribute的值为“1976-08-31”

如果我将模型更改为字符串,它可以正常工作但我的编辑器不会显示日期选择器

1 个答案:

答案 0 :(得分:0)

您需要对格式进行一些提示,以将它们解析回DateTime对象。

在ADUser构造函数中执行此操作...

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