Orijinal sorumlum here. Depo Kalıbı ve İş Birimi kullanarak veritabanından geri dönen sütun sayısını kısıtlamak için bir yol bulmaya çalıştığım yer. Benim sorum bir cevap buldu ama çözmedim ve şu andan itibaren yaşadığım problem olarak bu tekniği kullanarak ortaya çıkan yeni problemi göndermeye karar verdim tamamen yeni bir konu. Ben 'seçeneğini alamayan,Dynamic Linq Lambda ile Seç
IEnumerable<Models.Authors.Author> authors = unitOfWork.AuthorRepository.Get(filter: x => x.TenantID == 1);
Ama: benim kumandanın içine iyice
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<TEntity, TEntity> selector = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
aşağıdaki işleri: deposunda
, ben aşağıdaki parametreleri alan Get yöntemi var 'kısmı çalışma:IEnumerable<Models.Authors.Author> authors = unitOfWork.AuthorRepository.Get(filter: x => x.TenantID == 1, selector: a=> new { FirstName = a.FirstName });
ve benzer şekilde bu:
var authors = unitOfWork.AuthorRepository.Get(filter: x => x.TenantID == 1, selector: a=> new { FirstName = a.FirstName });
aşağıdaki iki hata veriyor:
Cannot implicitly convert type '<anonymous type: string FirstName>' to 'Models.Authors.Author'
ve
Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
Ben buna tamamen yabancı değilim. Yani gerçekten
GÜNCELLEME .. yardımına takdir ediyorum bu:
The entity or complex type 'DAL.Author' cannot be constructed in a LINQ to Entities query.
GÜNCELLEME 2: Yazar Sınıf // ad
IEnumerable<Models.Authors.Author> authors = unitOfWork.AuthorRepository.Get(filter: x => x.TenantID == 1, selector: a=> new Models.Authors.Author() { FirstName = a.FirstName });
bu verir
namespace Models.Authors
{
public class Author
{
[Key]
public int AuthorID { get; set; }
public int CategoryID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhotoPath { get; set; }
public byte[] PhotoBinary { get; set; }
public string PhotoPathOriginal { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Guid? PassGuid { get; set; }
public string Email { get; set; }
public string Twitter { get; set; }
public string Facebook { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateLastlogin { get; set; }
public int AuthorStatus { get; set; } // 0:inactive, 1:active, 2:suspended, 3:hidden etc.
public string AboutAuthor { get; set; }
public bool? RequirePublishApproval { get; set; }
public string ColumnName { get; set; }
public string ColumnImage { get; set; }
public bool? ChangePassAtFirstLogin { get; set; }
public int TenantID { get; set; }
public virtual AuthorCategory AuthorCategory { get; set; }
public virtual IQueryable<Post.Post> Posts { get; set; }
}
}
senin seçme işlemi için dönüş tipi 'Authors.Author' olacak değildir ... size Bir seçim yaptığınızda yeni, anonim bir tür yansıtıyorsunuz. Ne yapmaya çalıştığınıza bağlı olarak, yazarları "var" olarak bildirebilirsiniz. –
Dinamik LINQ kullanmayı düşünün. Ya da yinelenen bir soru olduğunu düşünüyorum [bu] (http://stackoverflow.com/a/723018/861716) bakın. –
Teşekkürler Gert ama benim için orayı sindirmek için çok fazla şey var. Basit bir görev gibi görünen şey, yazmak için çok fazla kod olması durumunda, depodan kurtulmaktan daha iyi olabilir. –