-4
public class Class1
{

    public int id { get; set; }
    public List<long?> degreeIds { get; set; }
    public long schoolId { get; set; }
}

public class Class2
{
    public int id { get; set; }

    public List<long> degreeIds { get; set; }
}

public class Class3
{
    public int id { get; set; }

    public long schoolId { get; set; }
}

I got the data from some where and I am looping with some data and assigning it not nullable one. cannot convert long? to long

bmdprasad
  • 65
  • 7
  • var filteredData = class1. .Where(x => x.schoolId == class3.schoolId && class2.degreeIds.Contains(x.degreeIds)).ToList(); – bmdprasad Nov 15 '19 at 15:35
  • 1
    "*I got the data from some where and I am looping with some data and assigning it not nullable one*" -- show this code, please – canton7 Nov 15 '19 at 15:35
  • var filteredData = class1. .Where(x => x.schoolId == class3.schoolId && class2.degreeIds.Contains(x.degreeIds)).ToList(); @canton7 while executing this type of linq it is getting issue – bmdprasad Nov 15 '19 at 15:36
  • 2
    @bmdprasad Please don't add further information in the comments, edit your question instead. – germi Nov 15 '19 at 15:36
  • @bmdprasad: Well, that makes sense. You can't convert a `long?` to a `long` because the former supports values which the latter does not. What exactly are you asking? – David Nov 15 '19 at 15:38
  • i just want to filter the values which contains class1.degreeids(x.degreeids) in calss2.degreeids. but here class2 degree ids are not nullable and class1.degreeids are nullable – bmdprasad Nov 15 '19 at 15:41
  • You can't do `.Contains(list)`. In your case, only `.Contains(long)` would work. Do you want to find out if *all* of the `x.degreeIds` exist in `class2.degreeIds` or if *any* of them exists? – Longoon12000 Nov 15 '19 at 15:43
  • @Longoon12000 I want all of the x.degreeids – bmdprasad Nov 15 '19 at 15:46

2 Answers2

4

I would check if the value is null, and ignore those where relevant.

List<long> list = degreeIds.Where(p => p.HasValue).Select(p => p.Value).ToList();
Kami
  • 19,134
  • 4
  • 51
  • 63
  • i just want to filter the values which contains class1.degreeids(x.degreeids) in calss2.degreeids. but here class2 degree ids are not nullable and class1.degreeids are nullable – bmdprasad Nov 15 '19 at 15:42
  • Wouldn't this still be an issue because youre still storing long? Objects into a list of Long without a cast? -- or can it go one way but not the other? – davedno Nov 15 '19 at 15:46
  • @bmdprasad I do not get that from your question - Care to edit your posted question with the additional requirements? – Kami Nov 15 '19 at 15:55
0

Since you said that you want to check if Class1 degreeIds contains all of the Class2 degreeIds, check out this: Check whether an array is a subset of another

I'm not sure on which object you're doing this .Where on but I assume it's a List<Class1> since x seems to be of type Class1. I'll call it class1List in this code.

Using this method you would then do:

var filteredData = class1List.Where(x =>
    x.schoolId == class3.schoolId &&
    !class2.degreeIds.Except(x.degreeIds
        .Where(deg => deg.HasValue)
        .Select(deg => deg.Value))
        .Any()).ToList();

This would select all long? that have a value (.HasValue) and then select the actual long value (.Value), remove all these values from the class2.degreesIds and check if there are any elements left. If there are none it means that class2.degreesIds had no values in it that didn't exist in x.degreeIds.

Longoon12000
  • 774
  • 3
  • 13