这是我的代码示例。
var prmsMySql = new List<MySqlParameter>();
var prms = new List<SqlParameter>();
foreach (var objSql in prms)
{
flag = false;
foreach (var mySql in prmsMySql)
{
if (Convert.ToString(objSql.Value) == Convert.ToString(mySql.Value))
{
flag = true;
break;
}
}
if (!flag)
{
break;
}
}
我需要查找两个列表是否具有相同的值,我的意思是prmsMySql值 和prms值(Sqlparameter和MySql参数中的值字段)。
此代码工作正常。是否有可能使linq实现使代码更短 并且易于阅读。
我尝试了以下查询。
var flag2 = prms.SelectMany(o => prmsMySql.Where(p => Convert.ToString(o.Value) !=
Convert.ToString(p.Value)).Distinct());
它给出了错误的答案。请帮帮我。 在此先感谢!!
答案 0 :(得分:1)
这样的事情应该有效:
prmsMySql.Any(x => prms.Any(p => p.Value.ToString() == x.Value.ToString());
或join
(from p1 in prmsMySql
join p2 in prms
on p1.Value.ToString() equals p2.Value.ToString()
select p1).Any()
答案 1 :(得分:0)
class MySqlParameter
{
public string Value { get; set; }
}
...
var lst1 = new List<MySqlParameter> {new MySqlParameter {Value="1"},
new MySqlParameter {Value="2"},
new MySqlParameter {Value="3"},
new MySqlParameter {Value="4"}};
var lst2 = new List<MySqlParameter> {new MySqlParameter {Value="1"},
new MySqlParameter {Value="2"},
new MySqlParameter {Value="3"},
new MySqlParameter {Value="7"}};
var res = from l1 in lst1
join l2 in lst2
on l1.Value equals l2.Value
select new { result = l1 };
bool flag = res.Count()==lst1.Count();
答案 2 :(得分:-1)
这是你内在的foreach循环的LINQ代码
foreach (var objSql in prms)
{
flag = prmsMySql.Any(mySql => Convert.ToString(objSql.Value) == Convert.ToString(mySql.Value));
if (flag)
continue;
break;
}
答案 3 :(得分:-1)
您是否尝试执行左外连接?
如何:执行左外连接(C#编程指南)
左外连接是一个连接,其中返回第一个集合的每个元素,无论它是否有任何相关 第二个集合中的元素。您可以使用LINQ执行左侧 通过在a的结果上调用DefaultIfEmpty方法来进行外连接 小组加入。
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
class Pet
{
public string Name { get; set; }
public Person Owner { get; set; }
}
public static void LeftOuterJoinExample()
{
Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };
Pet barley = new Pet { Name = "Barley", Owner = terry };
Pet boots = new Pet { Name = "Boots", Owner = terry };
Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
Pet bluemoon = new Pet { Name = "Blue Moon", Owner = terry };
Pet daisy = new Pet { Name = "Daisy", Owner = magnus };
// Create two lists.
List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };
var query = from person in people
join pet in pets on person equals pet.Owner into gj
from subpet in gj.DefaultIfEmpty()
select new { person.FirstName, PetName = (subpet == null ? String.Empty : subpet.Name) };
foreach (var v in query)
{
Console.WriteLine("{0,-15}{1}", v.FirstName + ":", v.PetName);
}
}
// This code produces the following output:
//
// Magnus: Daisy
// Terry: Barley
// Terry: Boots
// Terry: Blue Moon
// Charlotte: Whiskers
// Arlene:
修改此代码以对您的类进行操作;
如果符合以下条件,内容相同:
Count
与您的prms
集联接是一种导入的数据库工具。在可能的情况下,值得利用它。