के साथ LINQ के अंदर विशिष्टता LINQ क्वेरी के अंदर मैं अपने विनिर्देश का उपयोग करने की कोशिश कर रहा था। यहाँ परेशानी पैराम के साथ मेरे विनिर्देश के साथ है।ईएफ 4.3
आइए नकली एक सरल परिदृश्य:
public class Car {
public Guid Id { get; set; }
public string Color { get; set; }
public int UsedPieces { get; set; }
// whatever properties
}
public class Piece {
public Guid Id { get; set; }
public string Color { get; set; }
// whatever properties
}
public static class PieceSpecifications : ISpecification<Piece> {
public static ISpecification<Piece> WithColor(string color) {
return new Specification<Piece>(p => p.Color == color);
}
}
क्या मैं वास्तव में
// Get accepts ISpecification and returns IQueryable<Car> to force just one call to database
var carWithPieces = _carRepository.Get(CarSpecifications.UsedPiecesGreaterThan(10));
var piecesWithColor = from p in _pieceRepository.Get()
let car = carWithPieces.FirstOrDefault() // entire query will does one call to database
where PieceSpecifications.WithColor(car.Color).IsSatisfiedBy(p) // unfortunately it isn't possible
// where p.Color == car.Color -> it works, but it's not what I want
select p;
करने के लिए मुझे पता है यह एक छोटा सा भ्रामक है कोशिश कर रहा हूँ, लेकिन मैं का एक बहुत से बचने के लिए कोशिश कर रहा हूँ मेरे वास्तविक (बड़े) परिदृश्य के अंदर राउंडट्रिप्स और मुझे पता है कि वास्तव में कच्चे LINQ का उपयोग इकाई ढांचे के साथ करना असंभव है। मैं इतने सारे ब्लॉगों और असफल (मेरा) दृष्टिकोणों को आजमाने के लिए थक गया हूं। कोई असली वास्तविक दृष्टिकोण जानता है। ऐसा करने का एक और तरीका है?
त्रुटि
System.NotSupportedException: संस्थाओं को LINQ विधि 'बूलियन IsSatisfiedBy (App.Model.Piece)' विधि को नहीं पहचानता है, और इस विधि एक दुकान अभिव्यक्ति में अनुवाद नहीं किया जा सकता है ।
अद्यतन
बुनियादी विशिष्टता पैटर्न
public class Specification<T> : ISpecification<T> {
private readonly Expression<Func<T, bool>> _predicate;
public Specification(Expression<Func<T, bool>> predicate) {
_predicate = predicate;
}
public Expression<Func<T, bool>> Predicate {
get { return _predicate; }
}
public bool IsSatisfiedBy(T entity) {
return _predicate.Compile().Invoke(entity);
}
}
अद्यतन
यह बहुत आसान साफ है कि अगर मैं इस
// call to database
var car = _carRepository
.Get(CarSpecifications.UsedPiecesGreaterThan(10))
.FirstOrDefault();
// Whoah! look I'm working, but calling to database again.
var piecesWithColor = _pieceRepository
.Get(PieceSpecifications.WithColor(car.Color))
.ToArray();
012 कर
भंडार
// The Get function inside repository accepts ISpecification<T>.
public IQueryable<T> Get(ISpecification<T> specification) {
return Set.Where(specification.Predicate);
}
क्या आप समझा सकते हैं कि * क्या संतुष्ट है (पी) * करता है? – Aducci
मेरी पोस्ट की जांच करें, मैंने एक सरल विनिर्देश कार्यान्वयन के साथ अद्यतन किया। –
आप सही हैं, आप बस इसे नहीं कर सकते हैं। टी-एसक्यूएल में कस्टम कोड की व्याख्या करने के लिए लिंक इतने चालाक नहीं हैं। आइए 4.5 और ईएफ 5.0 के लिए प्रतीक्षा करें जहां enum समर्थन के साथ (यह सुविधा प्रश्न के साथ जुड़ती है) हम कुछ नई वाक्यविन्यास चीनी देख सकते हैं। मैं इसमें भी दिलचस्प हूं। –