使用DBContex从多个表中查询 [英] Query from Multiple table using DBContex

查看:81
本文介绍了使用DBContex从多个表中查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我在实体框架中更加重要我在使用两个表中的选择查询获得结果时遇到问题

有两个模型/表格Trip&评论

Hello,
I am biginner in Entity framework i am getting problem using select Query in Two table to get result
have two model/Tables Trip & Comments

Trips
tripId,trip_text,userId,createDate




Comments
comId,com_text,createDate,userID,tripID





现在我需要

tripId,trip_text,com_text,userID我如何得到这个结果



我尝试使用以下但无法获得结果所以请在这方面支持我。



now I need
tripId, trip_text, com_text, userID how do i get this result

I tried using below but unable to get Result so please support me in this regards.

using (var con = new DbCarContext())
            {
                List<string> list = con.Database.SqlQuery<string>("select a.tripID, a.tripText, b.commentText, a.userID from Trips a, Comments b where a.tripID=b.tripID and a.userID=a.userID).ToList();

              }





我的数据库上下文类是





my DB context class is

public class DbCarContext: DbContext
    {

        public DbCarContext() : base("DbCarContext")
        {
        }

        public DbSet<Comments> comments { get; set; }
        public DbSet<Trips> trips { get; set; }

    }

推荐答案

仔细查看您的查询
select a.tripID, a.tripText, b.commentText, a.userID from Trips a, Comments b where a.tripID=b.tripID and a.userID=a.userID



你不认为


Don't you think

select a.tripID, a.tripText, b.commentText, a.userID from Trips a, Comments b where a.tripID=b.tripID and a.userID=b.userID

可能更好


你有任何例外吗?



你可能需要加入:





左/右/内连接b中选择a.colum1,a.colum2



on a.column1 = b.column1

其中a.column2 ='what'



如果你提供有关您的表格的更具体的问题或详细信息,我可以帮助您查询。
Do you get any exception?

And you probably need to make join:

Select a.colum1, a.colum2
from a
left/right/inner join b
on a.column1 = b.column1
where a.column2= 'whatever'

if you give more specific question or details about your table I can help you with your query.


这篇关于使用DBContex从多个表中查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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