LINQ中的疑问-关于编写查询语句!!! [英] Doubt in LINQ - Regarding writing Query statements !!!

查看:114
本文介绍了LINQ中的疑问-关于编写查询语句!!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,
好吧,昨天,当我被问到这个问题时,我正在面试.我很困惑地回答.

问题是:

我们在LINQ中将查询语句编写为:
将"db"视为数据上下文类的对象.

Hello Friends,
Well yesterday I was appearing for an interview when this Question was asked to me for which I was confused to answer.

Question is:

We write query statements in LINQ as:
Consider ''db'' as an object of a data context class.

var q =from c in db.Customers select c;



那么,为什么我们要从"..中的c开始"编写查询,而不是简单地从中选择



So why do we write the query starting as "from c in ......." instead of simply writing as

Select * from db.Customers



我还回答了面试官说这是要编写的语法.但是他要我更深入地回答.



I also answered the interviewer that this is the Syntax to write.But he asked me to answer in more depth.

推荐答案

LINQ使编写SQL查询更加容易. br/> 对于简单的查询(如查询),LINQ没什么区别,但是如果SQL查询较长,则使用LINQ会更容易.
在这里看看:
http://www.linqpad.net/WhyLINQBeatsSQL.aspx [
LINQ is made to write SQL queries easier.
For simple queries, like your query, LINQ makes no difference, but if the SQL queries are longer, it''s easier to use LINQ.
Have a look here:
http://www.linqpad.net/WhyLINQBeatsSQL.aspx[^]


我相信"select * from"是数据库可以理解的语法.

linq旨在灵活地查询类似于数据库样式的对象.

但是选择"*"不会被linq理解.在上面的查询中,"from c" ==>从db.customers列表中的customer对象中选择一个客户.
可能这应该是一个带有更多过滤器的查询,以选择具有特定条件的特定客户.
i believe "select * from " is the syntax that database understands.

linq is intended to have the flexibility to query the objects similar to database style.

but select "*" will not be understood by linq. in the above query, "from c" ==> from customer object in the list of db.customers select one customer.
may be this should be a query with more filters on it to select a specific customer of certain criteria.


这篇关于LINQ中的疑问-关于编写查询语句!!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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