return 第一组 linq 查询属性作为可选字符串的最简单方法是什么?

What is the easiest way to return the first set of linq query properties as optional strings?

将第一组 linq 查询属性作为可选字符串或从类型匿名转换为 (string?, string?) 的最简单方法是什么?由于匿名对象,以下内容不起作用。

private (string? HotemName, string? TransportationName) Get3rdPartyHotelTranporationNames(int customerID, int supplierID, System.DateTime? startDate, System.DateTime? endDate) 
{

    var x = (from res in reservations
             join trans in _unitOfWork.tbl_reservations on res.id_pk equals transRes.id
             where trans.status == 4
             select new
             {
                 HotemName = res.Name,
                 TransportationName = trans.Name
             }).Take(1).Single();

    return x;
}

匿名类型与元组不同。

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.

C# tuples are types that you define using a lightweight syntax. The advantages include a simpler syntax, rules for conversions based on number (referred to as cardinality) and types of elements, and consistent rules for copies, equality tests, and assignments. As a tradeoff, tuples do not support some of the object-oriented idioms associated with inheritance.

此外,您不能 return 从函数中输入匿名类型。在您的情况下,您应该 returns 来自 LINQ 的新元组,如下面的代码所示:

private (string? HotemName, string? TransportationName) Get3rdPartyHotelTranporationNames(int customerID, int supplierID, System.DateTime? startDate, System.DateTime? endDate) 
{
    return (from res in reservations
             join trans in _unitOfWork.tbl_reservations on res.id_pk equals transRes.id
             where trans.status == 4
             select (res.Name, trans.Name)
             ).Take(1).Single();
}