LINQ to Entities无法识别方法'Int32 get_Item(Int32)'方法异常

时间:2013-09-26 13:01:23

标签: asp.net entity-framework

我有桌子:

游客

Tourist_ID

2.Name

Extra_Charges

1.Extra_Charge_ID
2.Description
3.Amount

Toutist_Extra_Charges

   1.Tourist_ID - foreign key

   2.Extra_Charge_ID - foreign key

所以我有一个带有ID的列表,我想用特定的id来记录游客的名字和他所有的额外费用(他们的描述和金额)。 因为我有很多很多关系,这是我的代码,它给了我以下例外:

List<int> reTouristID = new List<int>();
            reTouristID.Add(86);
            reTouristID.Add(87);

        for (int i=0;i<reTouristID.Count;i++)
        {
                  var db2 = new excursionEntities3();

        var tourist = db2.Tourist.Include("EXTRA_CHARGES").SingleOrDefault(t => t.Tourist_ID==reTouristID[i]);

                  if (tourist != null)
                  {

                          lblproba.Text += "Name " + tourist.Name_kir;
                  }

                    var extrachargess = tourist.EXTRA_CHARGES.Select(e => new {e.Extra_Charge_Description,e.Amout});
                    foreach (var extra in extrachargess)
                    {
                        lblproba.Text += "Des: " + extra.Extra_Charge_Description;
                        lblproba.Text += "AMM:" + extra.Amout;

                    }

           }

2 个答案:

答案 0 :(得分:2)

就在这一行:

SingleOrDefault(t => t.Tourist_ID==reTouristID[i])

引擎盖下,reTouristID[i]是一个方法get_Item,它是EF无法转换为SQL的众多CLR方法之一(它应该如何?)。解决方法很简单:

var index = reTouristID[i];
var tourist = db2.Tourist.Include("EXTRA_CHARGES")
                 .SingleOrDefault(t => t.Tourist_ID == index);

答案 1 :(得分:0)

 for (int i = 0; i < Data.Count(); i++)
                    {
                        var d = Data[i].Field;
                        var check = context.Data.Where(p => p.field.Contains(d)).ToList();
                        if (check.Count == 0)
                        {
                           // Do Your Stuff for Existing Data
                        }
                        else
                        {
                           // Do Your Stuff for Non-Existing Data
                        }
                   }