我试图弄清楚如何限制从linq查询中的相关子表中调用的字段。在此示例中为events表。事件表与联系人之间存在关系,因此当我尝试将其序列化以返回为json时,它会出现循环引用异常。如何在不将参考文献拉回联系的情况下仅从事件中提取信息?
var data = (from c in Contacts
.Where(a => a.LastName.Contains("Smith"))
let pa = c.Addresses.Where(a=>a.AddressType.Name.Contains("Primary")).FirstOrDefault ()
let e= c.Events.Take(5).OrderByDescending (x =>x.DateTime )
select new
{
c.Id,
value = c.LastName + ", " + c.FirstName ,
Street1 = pa.Street1,
Street2 = pa.Street2,
City = pa.City,
State = pa.State,
PostalCode = pa.PostalCode,
, Events= e
})
.Take(10).ToArray()