我有一个实体:
public class Branch
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int BranchID { get; set; }
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Postcode { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
我有一个 apicontroller 函数来返回分支对象:
IEnumerable<Branch> branches = null;
branches = repository.Branches.Where(b => b.LastModified > modifiedSince).AsEnumerable();
return branches;
我怎样才能只返回BranchID
和Name
?
我试过这个:
IEnumerable<Branch> branches = null;
branches = repository.Branches
.Where(b => b.LastModified > modifiedSince)
.Select(b => new Branch {BranchID = b.BranchID, Name = b.Name })
.AsEnumerable();
return branches;