【问题标题】:LINQ to SQL Query Array in List列表中的 LINQ to SQL 查询数组
【发布时间】:2014-02-14 09:00:39
【问题描述】:

我想知道是否有人可以帮助我解决一些小问题,我目前有一个项目列表,其中包括列表中的一个数组(我对此无能为力,数据是从 Web API 中提取的),我需要在该列表上运行 linq to sql 查询以遍历该列表中的数组以比较数组中的值,例如我的代码如下所示:

var query1 = from q1 in datacontext.view
             select q1;

foreach (var item in query1)
{
    try
    {
        var query2 = from q2 in List<>
                     where q2.Array(iterate through the array to find the name)
                             .value = item.Key(is a string)
                     select q2;

        if (query2.count() == 0)
        {
            Do something
        }
        else
        {
            Do something else
        }
    }
}

基本上,当它遍历第一个列表以比较值时,第二个查询中的列表基本上在数组中有一个数组,我需要找到数组的名称(而不是数字)并比较它的值在第一个查询中有一个值,希望这对某人有意义。

更新:确切的代码

List<net.autotask.webservices4.InstalledProduct> CurrentAutotaskSoftware = GetSoftwareAlreadyInAutotask(myService);

            LANSweeperDataContext ldc = new LANSweeperDataContext();

            var licenseKey = from l in ldc.LANSweeper_License_Keys
                             select l;

            foreach (var lssi in licenseKey)
            {

                try
                {
                    var lSLicenseKeys = from c in CurrentAutotaskSoftware
                                        where c.UserDefinedFields.Any(x => x.Value == lssi.License_Key)
                                        select c;



                    if (lSLicenseKeys.Count() == 0)
                    {
                        //AddItemToAutotask(myService, lssi);
                        Console.WriteLine("Adding " + lssi.Product + lssi.License_Key); 
                    }
                    else
                    {
                        //UpdateItemInAutotask(myService, lSLicenseKeys.First(), lssi);
                        Console.WriteLine("Updating with" + lssi.Product + lssi.License_Key);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

【问题讨论】:

    标签: c# sql arrays linq


    【解决方案1】:

    这就是你所追求的吗?

    var hasMatch= YourList.Any(x=>x.Value==item.Key);
    if(hasMatch)
    {
        //Do Something
    }
    else
    {
      //Do Something else
    }
    

    【讨论】:

    • 我在 linq to sql 中使用了它,但是它没有列举结果,有什么想法吗?
    • 您遇到了什么具体问题? from q2 in List&lt;&gt;,那个List&lt;&gt;来自数据库还是web api?
    • q1 来自数据库,q2 来自 web api
    • 那么,上面的代码有什么问题?它给出了一些错误或什么?
    • 不,它工作得很好,但是我需要将它合并到一个 LINQ to SQL 查询中,因为在将其插入过滤器“Where YourList. Any(x=>x.Value==item.Key)" 它说枚举没有产生任何结果
    猜你喜欢
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    相关资源
    最近更新 更多