简体   繁体   中英

Return a List<SomeObject> without a certain property using Linq in c#?

I have an object containing different properties like the object below:

 public class CompressedLogResponse
 {
    public string LoggerType { get; set; }
    public int NumberOfRegisters { get; set; }
    public int NewLogId { get; set; }
    public DateTime LoggerAnnounceTime { get; set; }
    public List<Log> Log{ get; set; }
 }

How can I return a List of this objekt that does not include the List<Log> Log property?

Linq would be preffered

Thanks for any help that you can provide

You can return an anonymous list of your original list like the following;

public static List<dynamic>   Test() {

            List<CompressedLogResponse> list = new List<CompressedLogResponse>();

            var result = list.Select(x => new
            { 
                x.LoggerAnnounceTime,
                x.LoggerType,
                x.NewLogId,
                x.NumberOfRegisters
            });

            return result.ToList<dynamic>();
        }

Take a look at the .Select() , and also the dynamic keyword.

Then to call it,

var x =  Test();
       foreach(dynamic o in x)
       {
           int NumberOfRegisters;

           //You have 2 ways... either by
           NumberOfRegisters = o.NumberOfRegisters;

           // or reflection
           NumberOfRegisters = o.GetType().GetProperty("NumberOfRegisters").GetValue(o, null);

       }

whith "does not include the List Log property" i guess you mean that the property "public List Log" will be blanked but still there, so you can just null that property out, because if you create an object that doesn't contain the "public List Log" property, than it will not be a "CompressedLogResponse" but will be another type.

List<CompressedLogResponse> listOne = new List<CompressedLogResponse>();
//....
//fill the listOne
//....
List<CompressedLogResponse> listWithoutListLog = (from item in listOne
                                      select new CompressedLogResponse(
                                            LoggerType = item.LoggerType,
                                            NumberOfRegisters = item.NumberOfRegisters ,
                                            NewLogId = item.NewLogId ,
                                            LoggerAnnounceTime = item.LoggerAnnounceTime ,
                                            Log= null)).ToList();

You cannot just hide a property of a class (you declared it a as public)

Option 1: Althought as Robson wrote you can set it null (thats not very reliable thaught cause nobody expects a class containing a property that is always null)

Option2: If you consume the class on the same place use a anonymous type as Mez wrote, althought it sounds like you want to hide the Property from external usage. (I don't like the dynamic approach, the dynamic keyword was made for interop/DOM not for transporting anonymous types.)

Option3: If you want a List of this type to be returned without the Log property, you have to create a new class (Inheritance is a good way to realize this):

public class CompressedLogResponseBase
{
    public string LoggerType { get; set; }
    public int NumberOfRegisters { get; set; }
    public int NewLogId { get; set; }
    public DateTime LoggerAnnounceTime { get; set; }
}

public class CompressedLogResponse : CompressedLogResponseBase
{
    public List<Log> Log{ get; set; }
}

Now you can return a list of base items (that do not have a Log property at all)

public List<CompressedLogResponseBase> ReturnWithoutLog(IEnumerable<CompressedLogResponse> items)
{
    return ((IEnumerable<CompressedLogResponseBase>)items).ToList();
}

If a IEnumerable as return type is suficient it becomes really easy

public IEnumerable<CompressedLogResponseBase> ReturnWithoutLog(IEnumerable<CompressedLogResponse> items)
{
    return items
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM