ठीक है, मैं एक नया उत्तर प्रस्तावित करूंगा (थोड़ा देर हो चुकी है, क्षमा करें) जो एसोसिएशन का नाम बदलता है, भले ही काम करेगा।
यह विधि मुख्य इकाई की एसोसिएशन संपत्ति की तलाश करेगी और फिर यह मास्टर टेबल में मान की तलाश करेगी। कल्पना कीजिए कि:
तालिका: Orders
Orders.CustomerID
द्वारा तालिका Customers
साथ संदर्भित के बराबर होती है Customers.Id
। इसलिए हम मुख्य तालिका की मेटा जानकारी, फ़ील्ड CustomerID
(जो संदर्भित फ़ील्ड है) और फ़ील्ड Name
(जो मान हम चाहते हैं) पास करते हैं।
/// <summary>
/// Gets the value of "referencedValueFieldName" of an associated table of the "fieldName" in the "mainTable".
/// This is equivalent of doing the next LINQ query:
/// var qryForeignValue =
/// from mt in modelContext.mainTable
/// join at in modelContext.associatedTable
/// on mt.fieldName equals at.associatedField
/// select new { Value = at.referencedValueField }
/// </summary>
/// <param name="mainTable">Metadata of the table of the fieldName's field</param>
/// <param name="fieldName">Name of the field of the foreign key</param>
/// <param name="referencedValueFieldName">Which field of the foreign table do you the value want</param>
/// <returns>The value of the referenced table</returns>
/// <remarks>This method only works with foreign keys of one field.</remarks>
private Object GetForeignValue(MetaTable mainTable, string fieldName, string referencedValueFieldName) {
Object resultValue = null;
foreach (MetaDataMember member in mainTable.RowType.DataMembers) {
if ((member.IsAssociation) && (member.Association.IsForeignKey)) {
if (member.Association.ThisKey[0].Name == fieldName) {
Type associationType = fPointOfSaleData.GetType();
PropertyInfo associationInfo = associationType.GetProperty(member.Name);
if (associationInfo == null)
throw new Exception("Association property not found on member");
Object associationValue = associationType.InvokeMember(associationInfo.Name, BindingFlags.GetProperty, null, fPointOfSaleData, null);
if (associationValue != null) {
Type foreignType = associationValue.GetType();
PropertyInfo foreignInfo = foreignType.GetProperty(referencedValueFieldName);
if (foreignInfo == null)
throw new Exception("Foreign property not found on assiciation member");
resultValue = foreignType.InvokeMember(foreignInfo.Name, BindingFlags.GetProperty, null, associationValue, null);
}
break;
}
}
}
return resultValue;
}
और कॉल:
AttributeMappingSource mapping = new System.Data.Linq.Mapping.AttributeMappingSource();
MetaModel model = mapping.GetModel(typeof(WhateverClassesDataContext));
MetaTable table = model.GetTable(typeof(Order));
Object value = GetForeignValue(table, "CustomerId" /* In the main table */, "Name" /* In the master table */);
समस्या यह है कि केवल केवल एक ही संदर्भित क्षेत्र के साथ विदेशी कुंजी के साथ काम करती है। लेकिन, कई चाबियों में बदलना काफी छोटा है।
यह मास्टर टेबल के किसी फ़ील्ड का मान प्राप्त करने का एक तरीका है, आप संपूर्ण ऑब्जेक्ट को वापस करने के लिए बदल सकते हैं।
पीएस: मुझे लगता है कि मैं अपनी अंग्रेजी के बारे में कुछ गलतियां करता हूं, यह मेरे लिए काफी मुश्किल है।
स्रोत
2012-05-22 05:51:43
इसे वास्तव में एक कदम आगे ले जाया जा सकता है और _OriginalPoorlyNamedStorageVariable का उपयोग करके पूरी तरह से छोड़ा जा सकता है। हालांकि मुझे यकीन नहीं है कि कन्स्ट्रक्टर में आपके स्वयं के स्टोरेज वैरिएबल को शामिल करने से कुछ अन्य जटिलताओं का कारण नहीं होगा, लेकिन शायद मैं वास्तव में जल्द ही पता चल रहा हूं ... – jahu