简体   繁体   English

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

[英]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 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. 所以我有一个带有ID的列表,我想要带有游客名字和所有带有指定ID的游客的额外费用(他们的描述和金额)。 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;

                    }

           }

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?). 在引擎盖下, reTouristID[i]是一个方法get_Item,它是EF无法转换为SQL的众多CLR方法之一(应该怎么做?)。 The work-around is simple: 解决方法很简单:

var index = reTouristID[i];
var tourist = db2.Tourist.Include("EXTRA_CHARGES")
                 .SingleOrDefault(t => t.Tourist_ID == index);
 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
                        }
                   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ to Entities无法识别方法&#39;Int32 Last [Int32] - LINQ to Entities does not recognize the method 'Int32 Last[Int32] LINQ to Entities无法识别方法&#39;Int32 ToInt32(Boolean)&#39; - LINQ to Entities does not recognize the method 'Int32 ToInt32(Boolean)' 使用枚举GetName方法时需要强制转换为Int32 - Casting to Int32 is required while using enum GetName method LINQ to Entities 无法识别“System.Object get_Item(System.String)”方法 - LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method 为什么List(Int32)的JSON序列化为ArrayList? - Why does List(of Int32) get JSON serialized as ArrayList? Guid 转换为 int32 - Guid converted to int32 转换为值类型&#39;Int32&#39;失败空值LINQ - The cast to value type 'Int32' failed null value LINQ 无效的转换异常从smallint转换为Int32时? - Invalid Cast Exception While converting from smallint to Int32? 无法调用操作方法&#39;System.Web.Mvc.ActionResult索引(Int32,Int32,System.String,System.String,System.String,Int32 ByRef) - Cannot call action method 'System.Web.Mvc.ActionResult Index(Int32, Int32, System.String, System.String, System.String, Int32 ByRef) 更新Int32列获取“输入字符串的格式不正确” - updating a Int32 column get “Input string was not in a correct format”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM