score:1
Accepted answer
var dogType = typeof(Dog);
var names = dogType.GetProperties()
.Where(x => Attribute.IsDefined(x, typeof(TableField)))
.Select(x => x.GetCustomAttribute<TableField>())
.Where(x => x.Inoculated == true)
.Select(x=>x.Name);
score:0
public static Dictionary<string, string> GetDogNames()
{
var namesDict = new Dictionary<string, string>();
var props = typeof(Dog).GetProperties();
foreach (PropertyInfo prop in props)
{
object[] attrs = prop.GetCustomAttributes(true);
foreach (object attr in attrs)
{
if (attr is TableField tableField && tableField.Inoculated)
{
string propName = prop.Name;
string auth = tableField.Name;
namesDict.Add(propName, auth);
}
}
}
return namesDict;
}
Source: stackoverflow.com
Related Query
- C#, filter custom attribute property values
- Use Linq to filter Hashtable based on a property of Values custom object
- Filter values based on custom attribute in another table
- How to filter linq query based on all of the values of one property from another list
- EFCore Filter on Property With Contains And List of Values
- C# LINQ select property if it has a custom attribute
- C# filter out duplicates from list of custom objects based on list attribute
- In LINQ, select all values of property X where X != null
- Adding A Custom Property To Entity Framework?
- Filter linq list on property value
- EntityFramework 5 filter an included navigation property
- List<T> Where T is custom object. Remove duplicates by property
- Getting unique values from a list of objects with a List<string> as a property
- Get the number of distinct property values in List<T> using LINQ in C#
- Filter two lists on one property c# using linq
- Find all classes with an attribute containing a specific property value
- C# LINQ Select objects with the same value of one property join values of other
- Get values by property name from an object at different levels
- C# Create object with dynamic properties : LINQ select List<object> values by property names array
- Rx filter by criterion and n values after criterion
- HashSet or Distinct to read distinct values of property in List<> of objects
- Writing a function to filter a list of a items by a property on it's base type in c#
- Entity Framework / Linq - Get distinct values of dynamically specified property
- How to use LINQ to filter property of child collection using (.Any())
- How to get an array of distinct property values from in memory lists?
- How to Group by values in IEnumerable Property
- Concatenate values of a property based on another property
- Combine property selector expression tree and value to create a predicate for EF filtering - create filter from lambda selector and value
- Getting property value based on its column attribute value
- XDocument/Linq concatenate attribute values as comma separated list
More Query from same tag
- Building lambda expression for filtering in list of dictionaries
- Is there a better way to optimize the sql query using Linq?
- Creating dynamically linq to EF query with OR predicates
- Cannot initialize TimeSpan with a collection initializer in linq query ¿Syntax error?
- Comparing two Lists and returning the distinct values and the differences
- LINQ to Entity: How to cast a complex type to a DTO
- Entity framework not inserting values into nullable datetime field
- Does not contain a definition for Where and no extension method?
- Insert Record into two tables with one to many relation by using linq to ado.net entity froamwork
- What can be causing Html.ValidateFor() method to produce a compile error?
- How to remove duplicates (distinct values) with out primary key
- LinQ XML mapping to a generic type
- How is using Entity + LINQ not just essentially hard coding my queries?
- Entity Framework query timeout but SQL is instant
- Distinct is not giving distinct row for table
- How do you use Linq to connect tables in different databases?
- VB.net Add new listview item in listview with Checked True?
- Find / Count Redundant Records in a List<T>
- Using Join to get the updated Table Linq Entity Framework
- Order by descending group by Max date, then order by single row in the group by date ascending using linq Method sintax
- How to create dynamic condition using Expression Tree
- aggregate two objects in a list into one
- How to get a single value from a Dictionary<Key,Value> given a property that belongs to the type Value
- Linq: Using ElementAt on a database
- Initialize a Jagged Array the LINQ Way
- How to order a list by property and group the elements in HTML Razor?
- Update a value by jQuery
- Error when combining Lambda expressions: "variable 'foo' of type 'Foo' referenced from the scope '', but it is not defined
- Populate custom List sub class from XML document via LINQ
- Do Static classes with static methods keep Entity Framework from opening too many connections