0

This is my code in a controller; im using MVC 5 ASP.NET

var x = db.MyEntity.Include(ds => ds.MyEntity1)
    .Where(ds =>
            (
                ds.MyEntity1.MyEntity12.x <= MyEntity1.MyEntity12.x
            &&
                ds.MyEntity1.MyEntity12.y>= MyEntity1.MyEntity12.x
            )
        ||
            (
                ds.MyEntity1.MyEntity12.x<= MyEntity1.MyEntity12.y
            &&
                ds.MyEntity1.MyEntity12.y>= MyEntity1.MyEntity12.y
            ))
    .Select(ds => ds.Emp);

    var finalEmp = db.Emp.Except(x).ToList();

Now i want to reuse this x variable. I want to separate it into a method and call it from anywhere. But I'm having doubts what kind of a return value i should use in that method, so it will work with Except().

Thanks in advance.

Jodrell
  • 34,946
  • 5
  • 87
  • 124
guitarlass
  • 1,587
  • 7
  • 21
  • 45

2 Answers2

0

Use IList.

Usually it is better to expose your custom objects via interfaces. This has been discussed here as well: Why is it considered bad to expose List<T>?

Community
  • 1
  • 1
banavalikar
  • 272
  • 1
  • 7
  • No i can't use that. I get `Error 3 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?) ` – guitarlass Aug 13 '15 at 09:13
  • tried that too `Unable to create a constant value of type 'MyDomain.Emp'. Only primitive types or enumeration types are supported in this context.` List wont work with except it seems. let me try Contains()? – guitarlass Aug 13 '15 at 09:36
  • Use IEqualityComparer as shown here: https://msdn.microsoft.com/en-us/library/bb300779.aspx – banavalikar Aug 13 '15 at 09:37
  • isn't there any other simple way i could achieve this? – guitarlass Aug 13 '15 at 10:06
  • Define: public static IEnumerable Except(this IEnumerable source, IEnumerable destination, Func comparer) { return source.Where(x => destination.Count(y => comparer(x, y)) == 0); } Then call it as: IList final = source.Except(destination, (x, y) => x. == y.).ToList(); – banavalikar Aug 13 '15 at 10:47
0

Well, maybe of being a novice in C# and ASP.NET, i couldn't work out with answers given by banavalikar. To be exact return type for the above method should be a IQueryable. However, because of the way this project is structured, this method is called from a different project. Therefore, it gave me in different context error. But, at the end I found a simple workaround to suite my need; Just returning a List of strings. (My EmpIds are strings) And using Contains() instead Except()

public static List<string> GetNotAvailEmp(Entity1 entityx)
    {
        using (MyDbContext db = new MyDbContext())
        {
            var x = db.MyEntity.Include(ds => ds.MyEntity1)
                .Where(ds =>
                    (
                      ds.MyEntity1.MyEntity12.x <= entityx.MyEntity12.x
                        &&
                      ds.MyEntity1.MyEntity12.y>= entityx.MyEntity12.x
                    )
                    ||
                    (
                     ds.MyEntity1.MyEntity12.x<= entityx.MyEntity12.y
                       &&
                     ds.MyEntity1.MyEntity12.y>= entityx.MyEntity12.y
                    ))
                   .Select(ds => ds.Emp.EmpId).ToList();
            return x;
        }
    }

And calling this from my controller;

List<string> x = MyDbQueryFile.GetNotAvailEmp(entityx);

var finalEmp= db.Emp.Where(s => !x.Contains(s.EmpId)).ToList();
guitarlass
  • 1,587
  • 7
  • 21
  • 45