Tania Marinova
Tania Marinova

Reputation: 1898

LINQ to Entities does not recognize the method 'Int32 get_Item(Int32)' method exception

I've got tables:

Tourists

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

So i've got a list with ids and I want to take tourist name and all of his extra_charges(their description and amount) of the tourists with specified ids. So as I have many to many relationship here is my code, which gives me the following exception:

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;

                    }

           }

Upvotes: 0

Views: 212

Answers (2)

cracker
cracker

Reputation: 4906

 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
                        }
                   }

Upvotes: 0

Gert Arnold
Gert Arnold

Reputation: 109109

It's in this line:

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

under the hood, reTouristID[i] is a method, get_Item, which is one of the many CLR methods that EF can't translate into SQL (how should it?). The work-around is simple:

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

Upvotes: 2

Related Questions