这段代码意味着什么? [英] what does this code means?

查看:76
本文介绍了这段代码意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static string dbFilePath = dataFolder +" \\database.mdf ;;

static string Security =; Integrated Security = True;

static string Datasource = @Data Source =(LocalDB

\ v11.0; AttachDbFilename =;



public static string connectionstring = Datasource + dbFilePath + Security;

public static LinqToSqlDataContext dc = new LinqToSqlDataContext(connectionstring);











private static int PhotoId()

{

while(true)

{

int n = new Random()。Next(9999);

if(!dc.ROOMPHOTOs.Select(c => c) .Number).Contains(n))

返回n;

}

}

dc是链接

i不理解这个陈述(c => c.Number)。

解决方案

ROOMPHOTO是某种类型的集合,我们不知道是什么让我们说MyType,所以它会像



List< mytype> < br $>


 ROOMPHOTOs.Select(c => c.Number)





Select语句将在ROOMPHOTO中的每个项目的括号内运行代码,项目将在该项目中运行被称为c所以,所以c.Number表示ROOMPHOTO集合中每个MyType的Number属性。



选择输出集合本身, ()中的所有内容都成为新的集合。所以Select(c => c.Number)将返回Number为的任何类型的集合。如果Number为int,则Select将返回IEnumerable< int>它将引用ROOMPHOTO集合中所有MyType项的所有Number属性的列表。



。如果集合中的任何项目(它正在选择所有数字属性的IEnumerable< int>列表中),则会返回true(n)匹配所以代码在功能上与此类似。



 foreach(在ROOMPHOTO中的MyType c)
{
if(c .Number == n)
{
return true;
}
}


这是一个lambda表达式。它可以解释为通过ROOMPHOTO循环


public static string dbFilePath = dataFolder + "\\database.mdf;;
static string Security = ";Integrated Security=True;
static string Datasource = @"Data Source=(LocalDB
\v11.0;AttachDbFilename=";

public static string connectionstring = Datasource + dbFilePath + Security;
public static LinqToSqlDataContext dc = new LinqToSqlDataContext( connectionstring );





private static int PhotoId()
{
while (true)
{
int n = new Random().Next( 9999 );
if (!dc.ROOMPHOTOs.Select( c => c.Number ).Contains( n ))
return n;
}
}
dc is link
i dont understand this statment (c => c.Number).

解决方案

ROOMPHOTOs is collection of some type, we don't know what so let's say MyType, so it will be something like

List<mytype>

ROOMPHOTOs.Select( c => c.Number )



The Select statement is going to run the code inside the brackets for each item in ROOMPHOTOs, where the item is going to be referred to as "c" so, so c.Number means "the Number property of each MyType in the ROOMPHOTOs collection".

Select outputs a collection itself, and everything in the ( ) is what becomes the new collection. So Select(c => c.Number) is going to return a collection of whatever type Number is. If Number is int then Select will return IEnumerable<int> which will reference a list of all the Number properties of all the MyType items in your ROOMPHOTOs collection.

.Contains(n) will return true if any item in the collection (it is acting on the Select so the IEnumerable<int> list of all the Number properties) matches n. So the code is similar in function to this

foreach (MyType c in ROOMPHOTOs)
{
    if (c.Number == n)
    {
        return true;
    }
}


That is a lambda expression. it can be explained as looping through ROOMPHOTOs


这篇关于这段代码意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆