LINQ to Entities does not recognize the method IsUserInCc
Clash Royale CLAN TAG#URR8PPP
LINQ to Entities does not recognize the method IsUserInCc
My Code:
elections = elections.Where(e => e.Creator == Username || e.Approver == Username || IsUserInCc(e.Cc,Username))
.OrderBy(e => e.Status)
.ThenByDescending(e => e.Group);
var test = elections.FirstOrDefault();
private bool IsUserInCc(string cc, string username)
var ccList = cc.Split(';');
if (ccList.Contains(username))
return true;
return LDAPUtility.Instance.IsUserInGroup(ccList.ToList(), username);
Error:
LINQ to Entities does not recognize method IsUserInCc.
From many posts, I can understand why error was thrown. Basically IsUserInCc is not available in SQL execution. I need somehow convert it back to C# to handle it.
LINQ to Entities does not recognize my method
LINQ to Entities does not recognize the method in query
LINQ to Entities does not recognize the method 'System.String ToString(Int32)'
However, in my specific case, what is the best approach?
Visit last Answer please!
– AmirReza-Farahlagha
Aug 6 at 7:18
what is the purpose of LDAPUtility.Instance.IsUserInGroup(ccList.ToList(), username);? if that can be removed or reworked the query can probably be compatible with IQueryable
– Mel Gerats
Aug 6 at 7:26
5 Answers
5
You need to convert to list
first. Also note that elections
must be able to hold a list
for this to run.
list
elections
list
elections = elections.ToList().Where(e => e.Creator == Username || e.Approver == Username || IsUserInCc(e.Cc,Username))
.OrderBy(e => e.Status)
.ThenByDescending(e => e.Group);
Can you try like this :
elections = elections.Where(e => e.Creator == Username || e.Approver == Username).Tolist().Where(e => IsUserInCc(e.Cc,Username))
.OrderBy(e => e.Status)
.ThenByDescending(e => e.Group);
var test = elections.FirstOrDefault();
private bool IsUserInCc(string cc, string username)
var ccList = cc.Split(';');
if (ccList.Contains(username))
return true;
return LDAPUtility.Instance.IsUserInGroup(ccList.ToList(), username);
This is different: it turns the last
||
into an &&
.– Gert Arnold
Aug 6 at 8:05
||
&&
For your function written in code, you cannot use that on Queryables
. You need to convert to in-memory list
and then apply the filter required using your function.
Queryables
list
So what if
elections
contains a million records?– Gert Arnold
Aug 6 at 8:03
elections
Then you need to convert whatever you are doing in this custom function to SQL function . No other go to handle this
– saravanakumar v
Aug 6 at 8:05
But that's impossible because of the
string.Split
. I don't think there is any feasible way along the lines of "fetch first, then filter". OP should change the data structure, as suggested in this answer.– Gert Arnold
Aug 6 at 8:09
string.Split
@GertArnold but that would be a design change . But if i am not wrong , the one involved here is an email Id separator (;) text box string , which cannot be normalized anymore deeper.
– saravanakumar v
Aug 6 at 9:23
Sure it can! The IDs shouldn't be stored as a semicolon-separated string.
– Gert Arnold
Aug 6 at 13:39
The root cause of your issue is that your underlying data isn't normalised properly. You need to put your CC's in a collection, not have them as a single deliniated string.
In SQL you'd need to add a new table called CC or something and put each user name in there and link it back to an election. Or if it's an in-memory collection, add a new property that in its Getter will do the split for you.
Either way, then you won't run into this kind of problem. If your data isn't properly structured, you will create problems for yourself further up the stack.
When you want to send request
to database
using Linq
like:
request
database
Linq
var query = listData.Where(x=>x.Id == 123);
Type of this query
is IQueryable
that means your query not Executed
yet!
query
IQueryable
Executed
Now you are sending data as IQueryable
to method and can not process on your data, you have to Execute
that with methods like: Tolist()
, ToListAsync()
or something like these.
IQueryable
Execute
Tolist()
ToListAsync()
The best way for these is that you get data from database without that method, after that you execute your query, you can Run this method.
GoodLuck.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Have you any Exception or something??
– AmirReza-Farahlagha
Aug 6 at 7:05