0

I'm learning Linq and using MVC. I have written a SQL query which I need to convert to a LINQ query.

select TokenID,TokenAsset,packet from TokenTable where id = 6 and packet = ''
and TokenID not in (select TokenID from TokenTable where id=6 and packet <> '')
group by TokenID,TokenAsset,Packet

I kindly ask help to convert the above query to a LINQ query. I know that the SQL query isn't efficient. It would better if you can help me to fix it.

2 Answers 2

1

Try this one:

var result = Tokens.Where(x=>x.Id==6 && 
                          x.Packet=="" &&
                          !Tokens.Exists(y=>y.TokenID==x.TokenID && 
                                            y.Id==6 && 
                                            y.Packet!="")
                   )
                   .GroupBy(x=>x.ID)
                   .ThenGroupBy(x=>x.TokenAsset)
                   .ThenGroupBy(x=>x.Packet);

Note I suppose that collection Tokens holds all the tokens you have.

Sign up to request clarification or add additional context in comments.

4 Comments

What happened to the not in part?
I don't think we need the not. If you look at sql query you will get what I mean. we need all tokens that have id==6 and their packet is empty. Actually, I think we want only these records. Otherwise the provided query doesn't make sense.
Are you saying that removing it will still give the same results? Notice that the "id" and "TokenID" seem to be two different fields.
Ah I see what you mean, thanks I am on correcting right now. Thanks !
0

Firstly your SQL query can just be

select distinct TokenID, TokenAsset, packet 
from TokenTable 
where id = 6 and packet = ''

the group by is not that useful since there are no aggregated columns. All selected columns are in the group by clause. Use distinct to achieve the same.

the secondary AND condition for tokenid is also redundant. It is exclusive to the first condition and hence doesn't change the result.

use this LINQ query:

var results = dbcontext.TokenTables
              .Where(t => t.id == 6 && t.Packet == "")
              .Select(t => new { t.TokenId, t.TokenAsset, t.Packet }).Distinct();

project only columns you need for performant calls by avoiding extra data transfer.

4 Comments

Suggesting to use distinct instead of group by is good. However, suggesting that the second condition is redundant without knowing the data structure can be incorrect. You don't know what values (or NULLs) the TokenID field has. Notice that the "id" and "TokenID" seem to be two different fields.
@Racil, couldn't correlate your comment with the query.. first part of query is all tokenIds which have id = 6 and packet ="", which means the tokenids for records with packet != "" will implicitly be excluded because of the AND conditions.. please let me know if i am missing something.. glad to understand and edit my understanding
Yes, the first condition will eliminate all the packet <> ''. However, the second condition may eliminate even more. For instance, you are assuming that "TokenID" is the key for the "TokenTable", but what if it is not? and what is "id"? If duplicate values for "TokenID" exist for some "id"s: let's say (id=6,ToketID=2,Packet=''),(id=6,TokenID=2,Packet='p'), the first condition will eliminate the second row, but then the second condition will eliminate the first row as well.
Racil latest comment is right. Will the Linq work as per the comments?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.