如何根据字符方便的从枚举类型中获取对应的值

如何根据字符方便的从枚举类型中获取对应的值,第1张

如果数值是尽可能连续的可以用一个数组存放对应的字符串

类似 char xxx[] = {"zero", "one", "two", "three",};

如果不是写一个函数类似

char f(int x)

{

switch (x)

{

case 1: return "one";

case 5: return "five";

case 10: return "ten";

}

return "unknown";

}

使用join来连接

EnumerableJoin<TOuter, TInner, TKey, TResult> 方法 (IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)

发送反馈

基于匹配键对两个序列元素进行关联。

使用默认的相等比较器对键进行比较。

命名空间: SystemLinq

程序集: SystemCore(在 SystemCoredll 中)

语法

--------------------------------------------------------------------------------

VBC#C++F#JScript以带有颜色区分的格式查看复制到剪贴板打印<ExtensionAttribute> _

Public Shared Function Join(Of TOuter, TInner, TKey, TResult) ( _

outer As IEnumerable(Of TOuter), _

inner As IEnumerable(Of TInner), _

outerKeySelector As Func(Of TOuter, TKey), _

innerKeySelector As Func(Of TInner, TKey), _

resultSelector As Func(Of TOuter, TInner, TResult) _

) As IEnumerable(Of TResult)

<ExtensionAttribute> _

Public Shared Function Join(Of TOuter, TInner, TKey, TResult) ( _

outer As IEnumerable(Of TOuter), _

inner As IEnumerable(Of TInner), _

outerKeySelector As Func(Of TOuter, TKey), _

innerKeySelector As Func(Of TInner, TKey), _

resultSelector As Func(Of TOuter, TInner, TResult) _

) As IEnumerable(Of TResult)

public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(

this IEnumerable<TOuter> outer,

IEnumerable<TInner> inner,

Func<TOuter, TKey> outerKeySelector,

Func<TInner, TKey> innerKeySelector,

Func<TOuter, TInner, TResult> resultSelector

)

public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(

this IEnumerable<TOuter> outer,

IEnumerable<TInner> inner,

Func<TOuter, TKey> outerKeySelector,

Func<TInner, TKey> innerKeySelector,

Func<TOuter, TInner, TResult> resultSelector

)

[ExtensionAttribute]

public:

generic<typename TOuter, typename TInner, typename TKey, typename TResult>

static IEnumerable<TResult>^ Join(

IEnumerable<TOuter>^ outer,

IEnumerable<TInner>^ inner,

Func<TOuter, TKey>^ outerKeySelector,

Func<TInner, TKey>^ innerKeySelector,

Func<TOuter, TInner, TResult>^ resultSelector

)

[ExtensionAttribute]

public:

generic<typename TOuter, typename TInner, typename TKey, typename TResult>

static IEnumerable<TResult>^ Join(

IEnumerable<TOuter>^ outer,

IEnumerable<TInner>^ inner,

Func<TOuter, TKey>^ outerKeySelector,

Func<TInner, TKey>^ innerKeySelector,

Func<TOuter, TInner, TResult>^ resultSelector

)

static member Join :

outer:IEnumerable<'TOuter>

inner:IEnumerable<'TInner>

outerKeySelector:Func<'TOuter, 'TKey>

innerKeySelector:Func<'TInner, 'TKey>

resultSelector:Func<'TOuter, 'TInner, 'TResult> -> IEnumerable<'TResult>

static member Join :

outer:IEnumerable<'TOuter>

inner:IEnumerable<'TInner>

outerKeySelector:Func<'TOuter, 'TKey>

innerKeySelector:Func<'TInner, 'TKey>

resultSelector:Func<'TOuter, 'TInner, 'TResult> -> IEnumerable<'TResult>

类型参数

TOuter

第一个序列中的元素的类型

TInner

第二个序列中的元素的类型。

TKey

键选择器函数返回的键的类型。

TResult

结果元素的类型。

参数

outer

类型: SystemCollectionsGenericIEnumerable <TOuter>

要联接的第一个序列。

inner

类型: SystemCollectionsGenericIEnumerable <TInner>

要与第一个序列联接的序列。

outerKeySelector

类型: SystemFunc <TOuter, TKey>

用于从第一个序列的每个元素提取联接键的函数。

innerKeySelector

类型: SystemFunc <TInner, TKey>

用于从第二个序列的每个元素提取联接键的函数。

resultSelector

类型: SystemFunc <TOuter, TInner, TResult>

用于从两个匹配元素创建结果元素的函数。

返回值

类型: SystemCollectionsGenericIEnumerable <TResult>

一个具有 TResult 类型元素的 IEnumerable<T>,这些元素是通过对两个序列执行内部联接得来的。

使用说明

在 Visual Basic 和 C# 中,可以在 IEnumerable<TOuter> 类型的任何对象上将此方法作为实例方法来调用。当使用实例方法语法调用此方法时,请省略第一个参数。有关更多信息,请参见 扩展方法 (Visual Basic)或 扩展方法(C# 编程指南)。

异常

--------------------------------------------------------------------------------

异常 条件

ArgumentNullException

outer 或 inner 或 outerKeySelector 或 innerKeySelector 或 resultSelector 为 null。

备注

--------------------------------------------------------------------------------

此方法通过使用延迟执行实现。

即时返回值为一个对象,该对象存储执行 *** 作所需的所有信息。

只有通过直接调用对象的 GetEnumerator 方法或使用 Visual C# 中的 foreach(或 Visual Basic 中的 For Each)来枚举该对象时,才执行此方法表示的查询。

使用默认的相等比较器 Default 对键进行哈希处理和比较。

联接是指基于某个公共键使两个信息源的元素相关联的 *** 作。

Join 在一个方法调用中产生两个信息源和使它们相匹配的密钥。

这与 SelectMany 的用法不同,后者需要一个以上的方法调用才可以执行相同的 *** 作。

Join 保留 outer 中的元素的顺序,并且对于这些元素中的每一个元素,保留 inner 中的匹配元素的顺序。

在查询表达式语法中, join (Visual C#) 或 Join (Visual Basic) 子句转换为 Join 的一个调用。

在关系数据库术语中, Join 方法实现内部同等联接。'

“内部”表示结果中仅包含在另一序列中具有匹配项的元素。

“同等联接”是在其中比较键是否相等的联接。

左外部联接 *** 作没有专用的标准查询运算符,但可以使用 GroupJoin 方法执行此 *** 作。

请参见 联接运算。

示例

--------------------------------------------------------------------------------

下面的代码示例演示如何使用 Join<TOuter, TInner, TKey, TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>) 基于公共键对两个序列执行内部联接。

VBC#C++F#JScript以带有颜色区分的格式查看复制到剪贴板打印Structure Person

Public Name As String

End Structure

Structure Pet

Public Name As String

Public Owner As Person

End Structure

Sub JoinEx1()

Dim magnus As New Person With {Name = "Hedlund, Magnus"}

Dim terry As New Person With {Name = "Adams, Terry"}

Dim charlotte As New Person With {Name = "Weiss, Charlotte"}

Dim barley As New Pet With {Name = "Barley", Owner = terry}

Dim boots As New Pet With {Name = "Boots", Owner = terry}

Dim whiskers As New Pet With {Name = "Whiskers", Owner = charlotte}

Dim daisy As New Pet With {Name = "Daisy", Owner = magnus}

Dim people As New List(Of Person)(New Person() {magnus, terry, charlotte})

Dim pets As New List(Of Pet)(New Pet() {barley, boots, whiskers, daisy})

' Create a list of Person-Pet pairs, where each element is an

' anonymous type that contains a Pet's name and the name of the

' Person that owns the Pet

Dim query = _

peopleJoin(pets, _

Function(person) person, _

Function(pet) petOwner, _

Function(person, pet) _

New With {OwnerName = personName, Pet = petName})

Dim output As New SystemTextStringBuilder

For Each obj In query

outputAppendLine(objOwnerName & " - " & objPet)

Next

' Display the output

MsgBox(outputToString)

End Sub

' This code produces the following output:

'

' Hedlund, Magnus - Daisy

' Adams, Terry - Barley

' Adams, Terry - Boots

' Weiss, Charlotte - Whiskers

Structure Person

Public Name As String

End Structure

Structure Pet

Public Name As String

Public Owner As Person

End Structure

Sub JoinEx1()

Dim magnus As New Person With {Name = "Hedlund, Magnus"}

Dim terry As New Person With {Name = "Adams, Terry"}

Dim charlotte As New Person With {Name = "Weiss, Charlotte"}

Dim barley As New Pet With {Name = "Barley", Owner = terry}

Dim boots As New Pet With {Name = "Boots", Owner = terry}

Dim whiskers As New Pet With {Name = "Whiskers", Owner = charlotte}

Dim daisy As New Pet With {Name = "Daisy", Owner = magnus}

Dim people As New List(Of Person)(New Person() {magnus, terry, charlotte})

Dim pets As New List(Of Pet)(New Pet() {barley, boots, whiskers, daisy})

' Create a list of Person-Pet pairs, where each element is an

' anonymous type that contains a Pet's name and the name of the

' Person that owns the Pet

Dim query = _

peopleJoin(pets, _

Function(person) person, _

Function(pet) petOwner, _

Function(person, pet) _

New With {OwnerName = personName, Pet = petName})

Dim output As New SystemTextStringBuilder

For Each obj In query

outputAppendLine(objOwnerName & " - " & objPet)

Next

' Display the output

MsgBox(outputToString)

End Sub

' This code produces the following output:

'

' Hedlund, Magnus - Daisy

' Adams, Terry - Barley

' Adams, Terry - Boots

' Weiss, Charlotte - Whiskers

在SQL中,枚举值是指一组有限的值,它们可以用来查询数据库中的特定数据。例如,如果您想要查询某个国家的所有城市,您可以使用枚举值来查询该国家的所有城市。您可以使用SELECT语句来查询枚举值,该语句的语法如下:SELECT FROM table_name WHERE column_name IN (value1, value2, value3, );其中,table_name是您要查询的表名,column_name是您要查询的列名,value1、value2、value3等是您要查询的枚举值。例如,如果您想要查询某个国家的所有城市,您可以使用以下SELECT语句:SELECT FROM cities WHERE country IN ('China', 'India', 'USA');这样,您就可以查询到指定国家的所有城市。

以上就是关于如何根据字符方便的从枚举类型中获取对应的值全部的内容,包括:如何根据字符方便的从枚举类型中获取对应的值、怎么用Linq链接两张数据表再查询出数据、sql传入枚举值获取不同数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9276835.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-26
下一篇2023-04-26

发表评论

登录后才能评论

评论列表(0条)

    保存