Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在LINQ/SQL中连接表_Sql_Linq_Linq To Sql - Fatal编程技术网

在LINQ/SQL中连接表

在LINQ/SQL中连接表,sql,linq,linq-to-sql,Sql,Linq,Linq To Sql,下面是一组行,每一行由productid、unitid、countryid组成。 我需要在相应的表格中找到每一行的详细信息(产品、单位、国家) 并返回具有相同格式的行 Id = product id or unitid or countryid name = proudct name or unit name or countryname modified = product updatedby or unit updated by or country uploadby 总之- 1. Fo

下面是一组行,每一行由productid、unitid、countryid组成。
我需要在相应的表格中找到每一行的详细信息(产品、单位、国家)

并返回具有相同格式的行

Id = product id or unitid or countryid
name = proudct name or unit name or countryname
modified = product updatedby or unit updated by or country uploadby
总之-

 1. For a Collection of rows
    a. use the id to get the extra details from the respective table
    b. return the same type of collection for the results
 2. do step 1 for 
    2.a For RegularToys (Run this logic on TableBigA)
    2.b For CustomToys(Run this logic on TableB)
 3. Return all the rows 
    by adding 2.a and 2.b

如何为此编写sql/linq查询?谢谢

没有任何进一步的细节,我无法更具体地说明以下情况,但我想你是在要求类似的东西。我假设您的Id是int(但如果不是,这很容易更改),并且您已经为所描述的表建立了实体数据模型

  • 为公共数据创建一个类:
  • class RowDetail { public int Id { get; set; } public string Name { get; set; } public string Modified { get; set; } } IEnumerable<RowDetail> products = from p in db.Products where <<CONDITION>> select new RowDetail() { Id = p.ProductId, Name = p.ProductName, Modified = p.UpdatedBy }; IEnumerable<RowDetail> units = from u in db.Units where <<CONDITION>> select new RowDetail() { Id = u.UnitId, Name = u.UnitName, Modified = u.UpdatedBy }; IEnumerable<RowDetail> countries = from c in db.Countries where <<CONDITION>> select new RowDetail() { Id = c.CountryId, Name = c.CountryName, Modified = c.UploadBy }; IEnumerable<RowDetail> results = products.Union(units).Union(countries); 类行详细信息 { 公共int Id{get;set;} 公共字符串名称{get;set;} 公共字符串已修改{get;set;} }

  • 将每个子表中的信息拉入新记录:
  • class RowDetail { public int Id { get; set; } public string Name { get; set; } public string Modified { get; set; } } IEnumerable<RowDetail> products = from p in db.Products where <<CONDITION>> select new RowDetail() { Id = p.ProductId, Name = p.ProductName, Modified = p.UpdatedBy }; IEnumerable<RowDetail> units = from u in db.Units where <<CONDITION>> select new RowDetail() { Id = u.UnitId, Name = u.UnitName, Modified = u.UpdatedBy }; IEnumerable<RowDetail> countries = from c in db.Countries where <<CONDITION>> select new RowDetail() { Id = c.CountryId, Name = c.CountryName, Modified = c.UploadBy }; IEnumerable<RowDetail> results = products.Union(units).Union(countries); 可数产品= 从p到db.乘积 哪里 选择 新的行详细信息() { Id=p.ProductId, Name=p.ProductName, 修改=p.UpdatedBy }; 可数单位= 从u开始,单位为db 哪里 选择 新的行详细信息() { Id=u.UnitId, 名称=u.UnitName, 修改=u.UpdatedBy }; 无数国家= 来自db国家/地区的c 哪里 选择 新的行详细信息() { Id=c.CountryId, Name=c.CountryName, 修改=c.UploadBy };

  • 最后,将所有记录合并到一个集合中:
  • class RowDetail { public int Id { get; set; } public string Name { get; set; } public string Modified { get; set; } } IEnumerable<RowDetail> products = from p in db.Products where <<CONDITION>> select new RowDetail() { Id = p.ProductId, Name = p.ProductName, Modified = p.UpdatedBy }; IEnumerable<RowDetail> units = from u in db.Units where <<CONDITION>> select new RowDetail() { Id = u.UnitId, Name = u.UnitName, Modified = u.UpdatedBy }; IEnumerable<RowDetail> countries = from c in db.Countries where <<CONDITION>> select new RowDetail() { Id = c.CountryId, Name = c.CountryName, Modified = c.UploadBy }; IEnumerable<RowDetail> results = products.Union(units).Union(countries); IEnumerable结果=产品。联合(单位)。联合(国家);


    我不确定这是否正是您想要的,因此如果需要进一步帮助,请随时提供反馈和/或更多详细信息。

    如果我理解正确,您希望使用给定的ID查找产品、单位或国家,但您不确定是哪一个。如果是这种情况,那么您可以构建如下延迟查询来查找给定实体:

    var prod = from p in db.Products
               where p.ProductId = id
               select new { Id = p.ProductId, Name = p.ProductName, Modified = p.UpdatedBy };
    
    var unit = from u in db.Units
               where p.UnitId = id
               select new { Id = u.UnitId, Name = u.UnitName, Modified = p.UpdatedBy };
    
    var ctry = from c in db.Countries
               where c.CountryId = id
               select new { Id = c.CountryId, Name = c.CountryName, Modified = c.UploadBy };
    
    然后执行查询,直到找到匹配的实体(其中
    为空合并运算符,如果左侧结果为
    null
    ,则返回右侧值)


    我正在努力完全理解你的问题。可能会浪费一些时间来给出一些示例或详细信息。如果您向我们提供您希望运行的SQL查询,我们可以轻松地将其转换为LINQ代码。很抱歉,我仍然无法完全理解。我猜您需要一个只包含三列的结果表:Id、name、modified。数据集可以是:(ProductId,“productA”,Date1)(CountryId,“USA”,Date2)(RegularToyId,“Ball”,Date3)(UnitId,“Unit18”,Date4)(CustomToyId,“manddeball”,Date5)是否正确?是的。您可以忽略我问题中的2和3个步骤,因为我需要在两个大表中运行相同的逻辑。所以我需要的数据集就是{id,name,date1}={productid,productname,updatedby}或者{id,name,date1}={unitid,unitname,updatedby}或者{id,name,date1}={countryid,countryname,uploadedby}诀窍是-如果我在products表中找到一个键,如果不签入units表,就得到细节-如果我们找到了细节,如果在product,units表中找不到,请将此信息用于输出,如果在任何表中找不到,请检查country表以了解详细信息返回{productid=id,name=null,Update by=null},这是可能的。谢谢,我为我的程序做了一些调整。谢谢你的帮助。