Reputation: 110
EDIT
The complete scenario is, I need to retrieve a list of objects (in this case is Person) and also apply a filter over the collection returned on the property Files, in short terms is I want to get only the persons who has more than one file of a specific type, but also I want to retrieve only this specific file from this query.
Object representation:
public class Person(){
public string name {get; set;}
public int id {get; set;}
public List<File> file {get; set;}
}
And file entity:
public class File(){
public string name {get; set;}
public int id {get; set;}
public int extension {get; set;}
public int type_file {get; set;}
}
OBS: i use EF6 and .net framework.
Example:
//returning a IQueryable
var t = service.getAll<person>()
.Where(x => x.id == id)
.Where(y => y.files.Any(l => l.type_file == 1))
.FirstOrDefault();
Upvotes: 0
Views: 150
Reputation: 110
This is my solution this problem, i thank you for all you help.
//var person is DTO
var person= base.Get(id);
var list = new List<FileDTO>();
person.file.ForEach(delegate (FileDTO obj)
{
if (obj.type_file== (int)TypeFile.Attachment)
{
obj.archive = null;
list.Add(obj);
}
});
person.file = list;
return person;
Upvotes: 0
Reputation: 5313
Since you didn't provide a Minimal, Complete and Verifiable Example, I've created a simple representation of your scenario:
static List<Person> GetAllPersons()
{
var ret = new List<Person>(new []
{
new Person(){ Id = 10 },
new Person(){ Id = 4 },
new Person(){ Id = 8 }
});
var rnd = new Random();
foreach(var person in ret)
for(int i = 0; i <3; i++)
person.Files.Add(new File() { FileType = rnd.Next(1,3) , Name= "File " + (i + 1).ToString() });
return ret;
}
class Person
{
public int Id { get; set; }
public List<File> Files { get;set; }
public Person()
{
Files = new List<File>();
}
}
class File
{
public int FileType { get; set; }
public string Name { get; set; }
}
If I understood your need well, you want to change the Files
property of the first Person
that matches to your restrictions.
It would be something like this:
public static void Main()
{
var Id = 4;
var all = GetAllPersons();
var t = all
.Where(x => x.Id == Id)
.Select(P => new Person()
{
Id = P.Id,
Files = P.Files.Where(l => l.FileType == 1).ToList()
})
.Where(y => y.Files.Count > 0)
.FirstOrDefault();
}
This complete example is available on dotnetfiddle
But for sure, such kind of logic makes no sense and it's wrong in all possible ways. I'm pretty confident that the problem you're facing is being raised from your confusing logic and it's getting even wrost.
I suggest you to organize your logic before going thru this dangerous way.
Upvotes: 1
Reputation: 24671
You are using FirstOrDefault
, which will return the first object found by an enumerable (or null
, if the list is empty*). If you want the list, remove this method call and add ToList
instead.
var t = service.getAll<person>()
.Where(x => x.id == id)
.Where(y => y.files.Any(l => l.type_file == 1))
.ToList();
*The more precise answer is that it will return the default value of whatever type the IEnumerable
represents. For value types, this will be the default value of that type (e.g. 0 for int
, false for bool
, etc). For reference types, this will be null
.
Upvotes: 0