如何获取List<匿名类型>的字段值(含类型转换)
Hey there! Let's break down how you can access the FILE_NAME, DATE, and REPORT_LINK properties from your anonymous type list, along with any necessary type conversions. Here are your best options, ordered by maintainability:
1. Best Practice: Use a Strongly-Typed Class (Recommended)
Anonymous types work great for quick, one-off operations, but if you need to work with this data beyond the immediate query, defining a dedicated class is the safest and most readable approach. It gives you compile-time checks and eliminates runtime errors from typos.
Step 1: Define your class
public class ReportSummary { public string FILE_NAME { get; set; } public DateTime DATE { get; set; } // Adjust the type to match your actual data (e.g., string if DATE is stored as text) public string REPORT_LINK { get; set; } }
Step 2: Modify your LINQ query to return the strongly-typed list
Instead of an anonymous type, instantiate your new class in the Select clause—this is also where you can handle type conversions upfront:
var reportList = SharedUnitOfWork.Context.ReportRequest .Where(item => item.IS_DELETED == 0 && item.TENANT_ID == TenantId && item.REPORT_STATE_ID == 2 && item.CREATED_BY_USE...) .Select(item => new ReportSummary { FILE_NAME = item.YOUR_FILE_NAME_PROPERTY, // Replace with your actual source property DATE = Convert.ToDateTime(item.YOUR_DATE_PROPERTY), // Convert directly here if needed REPORT_LINK = downloadLink + item.YOUR_REPORT_ID_PROPERTY // Build your link here }) .ToList();
Step 3: Access properties with full type safety
Now you can access fields directly without any guesswork:
foreach (var report in reportList) { string fileName = report.FILE_NAME; DateTime reportDate = report.DATE; // Already converted to the correct type string reportUrl = report.REPORT_LINK; // Use your values as needed! }
2. Quick Fix: Use dynamic Type
If you can't or don't want to define a class, using dynamic lets you access anonymous type properties directly. Just note that this skips compile-time checks—typos will cause runtime errors.
var anonymousList = SharedUnitOfWork.Context.ReportRequest .Where(...) .Select(item => new { FILE_NAME = item.YOUR_FILE_NAME_PROPERTY, DATE = item.YOUR_DATE_PROPERTY, REPORT_LINK = downloadLink + item.YOUR_REPORT_ID_PROPERTY }) .ToList(); foreach (dynamic item in anonymousList) { // Access properties and convert as needed string fileName = item.FILE_NAME; DateTime reportDate = Convert.ToDateTime(item.DATE); // Handle conversion here string reportLink = item.REPORT_LINK; }
3. Flexible but Verbose: Use Reflection
Reflection works if you need to access properties dynamically (e.g., if you don't know the property names at compile time). It's more verbose and has slight performance overhead, but it's useful for edge cases.
var anonymousList = SharedUnitOfWork.Context.ReportRequest .Where(...) .Select(item => new { FILE_NAME = item.YOUR_FILE_NAME_PROPERTY, DATE = item.YOUR_DATE_PROPERTY, REPORT_LINK = downloadLink + item.YOUR_REPORT_ID_PROPERTY }) .ToList(); foreach (var item in anonymousList) { var type = item.GetType(); // Get and convert each property string fileName = (string)type.GetProperty("FILE_NAME").GetValue(item); DateTime reportDate = Convert.ToDateTime(type.GetProperty("DATE").GetValue(item)); string reportLink = (string)type.GetProperty("REPORT_LINK").GetValue(item); }
Final Recommendation
Stick with the strongly-typed class whenever possible—it makes your code easier to maintain, test, and debug. Use dynamic only for short-lived, simple operations, and reflection only when you truly need dynamic property access.
内容的提问来源于stack exchange,提问作者Adventurer

