LINQ - 将结果放入字典(Of String,Integer),其中整数必须为0

时间:2011-09-19 08:10:11

标签: asp.net vb.net linq dictionary

我有一个LINQ2Entity问题,我想从数据库中获取值,字符串,所以我选择FProducts.Serial_number,为了结束查询,我做.ToDictionary

问题是,它告诉我它没有足够的参数来转换ToDictionary

所以我需要像select FProducts.Serial_number, Nothing). ToDictionary这样的东西。 FProducts.Serial_number, 0).ToDictionary也不起作用。

有什么建议吗?

Sub GetStatistics(ByVal ProductNumbers As List(Of String), ByRef Passed As Integer, ByRef FailedProducts As List(Of String))
        Passed = 0
        FailedProducts = Nothing
        Dim tpDictionary As Dictionary(Of String, Integer)
        For Each productNumber As String In ProductNumbers
            Using context As New SelmaEntities
                Dim Table = (From GoodProducts In context.TestResults
                            Where (GoodProducts.Art_no = productNumber)
                            Select GoodProducts.Art_no, GoodProducts.Failed)
                tpDictionary = (From FProducts In context.TestResults
                            Where (FProducts.Art_no = productNumber And FProducts.Failed <> 0)
                            Order By FProducts.Serial_number
                            Select FProducts.Serial_number, ).ToDictionary

            End Using
        Next
    End Sub

2 个答案:

答案 0 :(得分:4)

ToDictionary需要lambda expressions作为参数:一个用于选择字典键,一个用于选择值。所以试试:

tpDictionary = (From FProducts In context.TestResults
                 Where (FProducts.Art_no = productNumber And FProducts.Failed <> 0))
               .ToDictionary(Function(p) FProducts.Serial_number, Function(p) 0)

答案 1 :(得分:1)

var products =  from p in FProducts select new { value=serial_number,key=productid};
Dictionary<string, int> dictionary = new Dictionary<string, int>();
foreach(var item in products){
  dictionary.Add(item.value, item.key);
}