2017-05-03 71 views
0

我正在使用ASP.NET MVC,並且在使用實體框架在兩個表之間進行聯接時遇到問題。使用實體框架C與參考表進行聯接#

在這種情況下,我有一個正在查詢:

 return db.GENERIC_INGREDIENTS 
      .Where(genericIngredient => genericIngredient.STATUS_CODE_ID == enabledId) 
      .AsEnumerable() 
      .Select(genericIngredient => GenericIngredientToViewModelWithUnitLabel(genericIngredient, languageId)) 
      .OrderBy(genericIngredient => genericIngredient.Name); 

但我需要用表db.GENERIC_INGREDIENTS.INGREDIENTS一個連接,然後過濾器適用於CAN_BE_USED屬性。我嘗試了很多方法來做到這一點,但我遇到了問題。我想,我更接近我想要的查詢如下:

 return db.GENERIC_INGREDIENTS 
     .Where(genericIngredient => genericIngredient.STATUS_CODE_ID == enabledId) 
     .AsEnumerable() 
     .Select(genericIngredient => GenericIngredientToViewModelWithUnitLabel(genericIngredient, languageId)) 
     .Join(db.INGREDIENTS, 
      gi => gi.Id, 
      i => i.GENERIC_INGREDIENT_ID, 
      (i, gi) => new { GENERIC_INGREDIENTS = gi, INGREDIENTS = i }) 
     .OrderBy(genericIngredient => genericIngredient.Name) 

的GenericIngredientToViewModelWithUnitLabel如下:

public GenericIngredientsViewModel GenericIngredientToViewModelWithUnitLabel(GENERIC_INGREDIENTS genericIngredient, int languageId = 0) 
    { 
     DBEntities db = new DBEntities(); 
     var genericIngredientTranslation = ReadGenericIngredientsTranslation(genericIngredient, languageId); 
     var unitLabelTranslation = UnitLabelsService.Service.ReadUnitLabelTranslationById(db, genericIngredient.UNIT_LABEL_ID, languageId); 
     return new GenericIngredientsViewModel 
     { 
      Id = genericIngredient.GENERIC_INGREDIENT_ID, 
      StatusCodeId = genericIngredient.STATUS_CODE_ID, 
      UnitLabelId = genericIngredient.UNIT_LABEL_ID, 
      Name = genericIngredientTranslation.GENERIC_INGREDIENT_NAME + " (" + unitLabelTranslation.Name+ ")", 
      Description = genericIngredientTranslation.GENERIC_INGREDIENT_DESCRIPTION, 
      Languages = genericIngredient.GENERIC_INGREDIENTS_TRANSLATIONS.Select(translation => translation.LANGUAGE_ID).ToList() 
     }; 
    } 

在這第二個查詢將返回一個錯誤「匿名類型:主料GENERIC_INGREDIENTS ,GenericIngredientsViewModel不包含名稱的定義。「。由於GenericIngredientToViewModelWithUnitLabel返回名稱,所以這對我來說沒有多大意義。此查詢必須返回一個IEnumerable。

感謝您的任何幫助。

回答

0

我認爲你必須修改的最後一行,以獲取正確的名稱:

return db.GENERIC_INGREDIENTS 
    .Where(genericIngredient => genericIngredient.STATUS_CODE_ID == enabledId) 
    .AsEnumerable() 
    .Select(genericIngredient => GenericIngredientToViewModelWithUnitLabel(genericIngredient, languageId)) 
    .Join(db.INGREDIENTS, 
     gi => gi.Id, 
     i => i.GENERIC_INGREDIENT_ID, 
     (i, gi) => new { GENERIC_INGREDIENTS = gi, INGREDIENTS = i }) 
    .OrderBy(output => output.GENERIC_INGREDIENTS.Name) 
+0

那不要工作:(。同樣的錯誤 –

+0

@DanielSerrão上面提到的現有查詢是否工作?如果是這樣,那麼根據查詢'.Name'是你在最後命令的內容,它是屬性o你的'GenericIngredientsViewModel'類。根據你的錯誤文本,它看起來像最後一個order by子句不能訪問'.Name'屬性,因爲新創建的匿名類型沒有它,所以上面的答案應該工作。你可以嘗試省略'OrderBy'子句來確認錯誤是由於它嗎?此外如果可能的話分享確切的錯誤信息。 – degant

0

爲了解決不承認我只需要改名字的問題:

gi => gi.Id, 
    i => i.GENERIC_INGREDIENT_ID, 
    (i, gi) => new { GENERIC_INGREDIENTS = gi, INGREDIENTS = i }) 

gi => gi.GENERIC_INGREDIENT_ID, 
    i => i.GENERIC_INGREDIENT_ID, 
    (gi, i) => new { GENERIC_INGREDIENTS = gi, INGREDIENTS = i }) 

問題是參數的順序,當做「OrderBy(output => output.GENERI C_INGREDIENTS.Name)「,我實際上試圖從不存在的INGREDIENTS中獲取名稱。

但現在我有一個問題,結果返回許多重複。當前查詢如下:

 return db.GENERIC_INGREDIENTS 
     .Join(db.INGREDIENTS, 
      gi => gi.GENERIC_INGREDIENT_ID, 
      i => i.GENERIC_INGREDIENT_ID, 
      (gi, i) => new { GENERIC_INGREDIENTS = gi, INGREDIENTS = i }) 
     .Where(output => (output.GENERIC_INGREDIENTS.STATUS_CODE_ID == enabledId) 
          && (output.INGREDIENTS.CAN_BE_USED == true) 
     .AsEnumerable() 
     .Select(output => GenericIngredientToViewModelWithUnitLabel(output.GENERIC_INGREDIENTS, languageId)) 
     .OrderBy(genericIngredient => genericIngredient.Name); 

此查詢沒有任何錯誤並返回正確的類型IEnumerable,但它返回許多重複項。我試着在SQL Server中的相同查詢,它的作品完美:(

0

問題已解決。問題是,它存在許多不同的成分爲同一generic_ingredient。要解決在SQL Server中我需要使用generic_ingredient_id「NOT IN」 ingredients_ids,爲了讓使用EF,我們可能需要使用‘包含’我沒有最終的解決辦法,因爲我做到了使用存儲過程。

感謝您的幫助。