question

MohammadQasim-1438 avatar image
1 Vote"
MohammadQasim-1438 asked MohammadQasim-1438 commented

How to make "Not Equal " using Linq


Hi,
using Sharepoint 2016 On Prem

I have created Linq query to extract data from 2 lists,

Suppose have 2 lists :
List A and , List B


Solution Required: want to retrive those data from list A ,which is not present on list B.

attached is the code image
88272-querymapwf.png
thanks


office-sharepoint-server-development
querymapwf.png (62.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Jerryzy avatar image
0 Votes"
Jerryzy answered MohammadQasim-1438 commented

Hi @MohammadQasim-1438 ,

If want to filter out resultUser "m_userid" not equal to resultwfUser "m_wfID", modify to this:

 var ListC = resultUser.Select(u=>u.m_userid).Except(resultwfuser.Select(x => x.m_wfID));

But the above will only return m_userid as using Select.

Another better way will like this:

 var ListD = resultUser.Where(a => !resultwfuser.Select(b => b.m_wfID).Contains(a.m_userid));

This will return the who field which meets condition.

Reference:

Is there a “not equal” in a linq join

Thanks
Best Regards



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

its fetching those records which are available on this "resultwfuser"

,it should not fetch those records which are available on 'resultwfuser"
like Not equal

below image is for reference

89044-userid.png


0 Votes 0 ·
userid.png (50.8 KiB)
Jerryzy avatar image Jerryzy MohammadQasim-1438 ·

You should compare m_userid with m_wfID based on your original question. Why you want to compare with x.m_wfusername[0].ToString() now ?

0 Votes 0 ·
Jerryzy avatar image Jerryzy MohammadQasim-1438 ·

var ListD = resultUser.Where(a => !resultwfuser.Select(b => b.m_wfID).Contains(a.m_userid));

0 Votes 0 ·
Jerryzy avatar image Jerryzy MohammadQasim-1438 ·

Capture for your reference:

resultUser (m_userid 1,2,3,4):

88929-snipaste-2021-04-19-16-20-38.png

resultWfUser:(m_wfID 1,2,3)

89016-snipaste-2021-04-19-16-20-50.png

Result:

89017-snipaste-2021-04-19-16-21-02.png

4 is in resultUser and not in resultWfUser, this should be what you want.


0 Votes 0 ·

Appreciate supporst and help.
I would like to clear some points here, please allow me

Point 1 ) I want to fetch those record which is not available on list or var "'resultwfuser" like


If "resultUser" has 1,2,3,4,5 and list 'resultwfuser has 3,4

so final var/list has 1,2,5 ID , because it doesnot exist in "'resultwfuser ".

Point # 2
I have to take this "_wfusername[0].ToString() now " because it contains user Forein key/look up of "resultUser" ,

"m_wfID" it contains ID of other list not Lookup ,so we cannot match not filter records accorindlgy.

Thanks

0 Votes 0 ·
Show more comments
cooldadtx avatar image
0 Votes"
cooldadtx answered cooldadtx commented

Use the Except method to return everything in one list that is not in the second list.

var listA = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var listB = new[] { 2, 4, 6, 8, 10, 12, 14 };

var listC = listA.Except(listB);


Note that it uses equality comparison, like Union and other methods, so it is possible you'll need to define a custom IEqualityComparer implementation to do the comparison based upon the rules you want to use.

In your case you're trying to do this via a join. As discussed in the docs join is equality only. To do a non-equality you can refer to this article but I think Except is easier.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

giving me below error

88483-errorlin.png


0 Votes 0 ·
errorlin.png (40.7 KiB)

The IEnumerable<T> on the left side must be the same type as the one on the right. That doesn't appear to be the case here. It appears you are using anonymous types so ensure the resultUser and resultwfuser anon types have the same members and then the compiler will use the same type. Alternatively create a regular type and use that for both resultUser and resultwfuser and then the compiler will be happy. You'll need to define an equality comparer though as reference equality would apply here and they would never match.

Unfortunately the site is generating 403s when posting code for this thread right now so I cannot post the relevant code. But implement IEqualityComparer and have both methods use just the field you care about (m_userId) and it should work.

0 Votes 0 ·