tcmauldin
tcmauldin

Reputation: 15

'ArrayIndex' is not supported in LINQ to Entities with a complex where statement

I am new to Linq and hope someone can help me. I know this question has probable already been answered somewhere but I can't find it so here goes. I am trying to retrieve one record that matches the name of the person. Here is my code. when I use just one variable in the where statement the code works fine but when there are more than one I get the array index error. Can anyone please straighten me out on what I am doing wrong.

var test = (from b in _context.UserInfos
   where b.FirstName == splitCacId[0] && b.MiddleInitial == splitCacId[1] && b.FirstName == splitCacId[2]
   select b).FirstOrDefault();

Upvotes: 0

Views: 191

Answers (1)

Rutuja
Rutuja

Reputation: 460

If you use an array index (splitCacId[0]) inside an expression tree, it tries to convert that into an expression as well.

Just work around it by using a separate variable

Use following code instead of accessing array index in linq directly.

var firstName = splitCacId[0];
var MiddleInitial= splitCacId[1];
var lastName = splitCacId[2];

var test = (from b in _context.UserInfos
   where b.FirstName == firstName && b.MiddleInitial == MiddleInitial && b.FirstName == lastName 
   select b).FirstOrDefault();

Upvotes: 1

Related Questions